Skip to content

Commit

Permalink
Add clear_cache method to SQLiteCache
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Sep 5, 2024
1 parent adfee94 commit bfaee2b
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions genshin/client/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ def __init__(self, conn: aiosqlite.Connection, *, ttl: int = HOUR, static_ttl: i
self.ttl = ttl
self.static_ttl = static_ttl

async def _clear_cache(self) -> None:
"""Clear timed-out items."""
now = time.time()

await self.conn.execute("DELETE FROM cache WHERE expiration < ?", (now,))
await self.conn.commit()

async def initialize(self) -> None:
"""Initialize the cache."""
await self.conn.execute(
Expand Down Expand Up @@ -254,6 +261,8 @@ async def set(self, key: typing.Any, value: typing.Any) -> None:
)
await self.conn.commit()

await self._clear_cache()

async def get_static(self, key: typing.Any) -> typing.Optional[typing.Any]:
"""Get a static object with a key."""
return await self.get(key)
Expand All @@ -265,3 +274,5 @@ async def set_static(self, key: typing.Any, value: typing.Any) -> None:
(self.serialize_key(key), self.serialize_value(value), int(time.time() + self.static_ttl)),
)
await self.conn.commit()

await self._clear_cache()

0 comments on commit bfaee2b

Please sign in to comment.