jabajke

Untitled

Mar 21st, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import redis.asyncio as aioredis
  2.  
  3. from .settings import settings
  4.  
  5. redis_client = aioredis.Redis(
  6. host=settings.redis_settings.REDIS_HOST,
  7. port=settings.redis_settings.REDIS_PORT,
  8. db=settings.redis_settings.REDIS_DB
  9. )
  10.  
  11.  
  12. def cache(func, seconds: int = 60 * 5):
  13. async def wrapper(*args, **kwargs):
  14. key = f'{func.__name__}:{args}:{kwargs}'
  15. result = await redis_client.get(key)
  16. if result is None:
  17. result = await func(*args, **kwargs)
  18. redis_client.set(key, result, ex=seconds)
  19. return result
  20.  
  21. return wrapper
  22.  
  23. @router.get(
  24. '/api-data',
  25. status_code=status.HTTP_200_OK
  26. )
  27. @cache
  28. async def api_data(
  29. service: OutputDataService = Depends(),
  30. limit: int = 10,
  31. ):
  32. return service.output_api_data(limit)
  33.  
Advertisement
Add Comment
Please, Sign In to add comment