Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import redis.asyncio as aioredis
- from .settings import settings
- redis_client = aioredis.Redis(
- host=settings.redis_settings.REDIS_HOST,
- port=settings.redis_settings.REDIS_PORT,
- db=settings.redis_settings.REDIS_DB
- )
- def cache(func, seconds: int = 60 * 5):
- async def wrapper(*args, **kwargs):
- key = f'{func.__name__}:{args}:{kwargs}'
- result = await redis_client.get(key)
- if result is None:
- result = await func(*args, **kwargs)
- redis_client.set(key, result, ex=seconds)
- return result
- return wrapper
- @router.get(
- '/api-data',
- status_code=status.HTTP_200_OK
- )
- @cache
- async def api_data(
- service: OutputDataService = Depends(),
- limit: int = 10,
- ):
- return service.output_api_data(limit)
Advertisement
Add Comment
Please, Sign In to add comment