-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Original media url cache added
BREAKING CHANGE: Redis dependency
- Loading branch information
1 parent
5c4e3dd
commit 62a3f4d
Showing
9 changed files
with
141 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import asyncio | ||
from functools import wraps | ||
from typing import Awaitable, Callable, TypeVar | ||
from typing_extensions import ParamSpec | ||
|
||
from redis.asyncio.client import Redis | ||
|
||
|
||
P = ParamSpec("P") | ||
T = TypeVar("T") | ||
|
||
|
||
class AsyncRedisCache: | ||
prefix: str | ||
redis: Redis | ||
|
||
@classmethod | ||
def init(cls, redis: Redis, prefix: str) -> None: | ||
cls.redis = redis | ||
cls.prefix = prefix | ||
|
||
@classmethod | ||
def aredis_cache( | ||
cls, | ||
key_generator: Callable[P, str], | ||
cache_serializer: Callable[[T], str], | ||
cache_deserializer: Callable[[str], T], | ||
ttl: int, | ||
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]: | ||
def decorator( | ||
func: Callable[P, Awaitable[T]] | ||
) -> Callable[P, Awaitable[T]]: | ||
lock = asyncio.Lock() | ||
|
||
@wraps(func) | ||
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: | ||
cache_key = cls.prefix + key_generator(*args, **kwargs) | ||
|
||
async with lock: | ||
cache_value = await cls.redis.get(cache_key) | ||
if cache_value is not None: | ||
if isinstance(cache_value, bytes): | ||
cache_value = cache_value.decode() | ||
|
||
return cache_deserializer(cache_value) | ||
|
||
res = await func(*args, **kwargs) | ||
await cls.redis.setex( | ||
cache_key, ttl, cache_serializer(res) | ||
) | ||
return res | ||
|
||
return wrapper | ||
|
||
return decorator |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters