Skip to content

Commit

Permalink
Merge pull request #191 from Krukov/fix-etag-generatio
Browse files Browse the repository at this point in the history
fix etag generation for mem or disk backend #190
  • Loading branch information
Krukov authored Jan 22, 2024
2 parents 95e16bd + 155468a commit 9b1b127
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cashews/contrib/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from cashews import Cache, Command, cache, invalidate_further
from cashews._typing import TTL
from cashews.picklers import DEFAULT_PICKLER

_cache_max_age: ContextVar[int] = ContextVar("cache_control_max_age")

Expand All @@ -25,7 +26,6 @@
_NO_CACHE = "no-cache" # disable GET
_NO_STORE = "no-store" # disable GET AND SET
_MAX_AGE = "max-age="
_ONLY_IF_CACHED = "only-if-cached"
_PRIVATE = "private"
_PUBLIC = "public"

Expand Down Expand Up @@ -140,6 +140,11 @@ def set_callback(key: str, result: Any):
async def _get_etag(self, key: str) -> str:
data = await self._cache.get_raw(key)
expire = await self._cache.get_expire(key)
if not isinstance(data, bytes):
if isinstance(data, Response):
data = data.body
else:
data = DEFAULT_PICKLER.dumps(data)
etag = blake2s(data).hexdigest()
await self._cache.set(etag, True, expire=expire)
return etag
Expand Down
3 changes: 3 additions & 0 deletions cashews/picklers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ def get_pickler(name: str):
raise UnsupportedPicklerError()

return _picklers[name]


DEFAULT_PICKLER = get_pickler(DEFAULT_PICKLE)

0 comments on commit 9b1b127

Please sign in to comment.