Skip to content

Commit

Permalink
chore: replace diskcache with diskcache2 to improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Nov 29, 2024
1 parent d5d9302 commit c0055ec
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic_core import Url

from src.api import get
from src.utils.db import TypedDB
from src.utils.db import Cache

app = FastAPI()

Expand All @@ -26,7 +26,7 @@ def get_url(url: Url):
return str(url)


db = TypedDB[Response]("v1")
db = Cache[Response]("v1")


@app.get("/proxy")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires-python = ">=3.12"
dependencies = [
"brotli-asgi~=1.4.0",
"curl-cffi~=0.7.3",
"diskcache~=5.6.3",
"diskcache2~=0.1.1",
"fake-useragent~=1.5.1",
"fastapi~=0.115.5",
"httptools~=0.6.4",
Expand Down
19 changes: 6 additions & 13 deletions src/utils/db.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
from pathlib import Path

cache_root = Path(__file__, "../../../cache").resolve()
from diskcache import Cache as BaseCache

cache_root = Path(__file__, "../../../cache").resolve()

class TypedDB[T]:
def __init__(self, name: str):
from diskcache import Cache

path = cache_root / name
class Cache[V](BaseCache[str, V]):
def __init__(self, directory: str):
path = cache_root / directory
path.mkdir(parents=True, exist_ok=True)

self.cache = Cache(path)

def __getitem__(self, key: str) -> T:
return self.cache[key] # type: ignore

def __setitem__(self, key: str, value: T):
self.cache[key] = value
super().__init__(path)

0 comments on commit c0055ec

Please sign in to comment.