|  | 
|  | 1 | +"""Decorator for API Cache.""" | 
|  | 2 | + | 
|  | 3 | +from functools import wraps | 
|  | 4 | +from http import HTTPStatus | 
|  | 5 | + | 
|  | 6 | +from django.conf import settings | 
|  | 7 | +from django.core.cache import cache | 
|  | 8 | +from django.http import HttpRequest | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +def generate_key( | 
|  | 12 | +    request: HttpRequest, | 
|  | 13 | +    prefix: str, | 
|  | 14 | +): | 
|  | 15 | +    """Generate a cache key for a request.""" | 
|  | 16 | +    return f"{prefix}:{request.get_full_path()}" | 
|  | 17 | + | 
|  | 18 | + | 
|  | 19 | +def cache_response( | 
|  | 20 | +    ttl: int | None = None, | 
|  | 21 | +    prefix: str | None = None, | 
|  | 22 | +): | 
|  | 23 | +    """Cache API responses for GET and HEAD requests. | 
|  | 24 | +
 | 
|  | 25 | +    Args: | 
|  | 26 | +        ttl (int): The time-to-live for the cache entry in seconds. | 
|  | 27 | +        prefix (str): A prefix for the cache key. | 
|  | 28 | +
 | 
|  | 29 | +    """ | 
|  | 30 | +    if ttl is None: | 
|  | 31 | +        ttl = settings.API_CACHE_TIME_SECONDS | 
|  | 32 | + | 
|  | 33 | +    if prefix is None: | 
|  | 34 | +        prefix = settings.API_CACHE_PREFIX | 
|  | 35 | + | 
|  | 36 | +    def decorator(view_func): | 
|  | 37 | +        @wraps(view_func) | 
|  | 38 | +        def _wrapper(request, *args, **kwargs): | 
|  | 39 | +            if request.method not in ("GET", "HEAD"): | 
|  | 40 | +                return view_func(request, *args, **kwargs) | 
|  | 41 | + | 
|  | 42 | +            cache_key = generate_key( | 
|  | 43 | +                request=request, | 
|  | 44 | +                prefix=prefix, | 
|  | 45 | +            ) | 
|  | 46 | +            if response := cache.get(cache_key): | 
|  | 47 | +                return response | 
|  | 48 | + | 
|  | 49 | +            response = view_func(request, *args, **kwargs) | 
|  | 50 | +            if response.status_code == HTTPStatus.OK: | 
|  | 51 | +                cache.set(cache_key, response, timeout=ttl) | 
|  | 52 | +            return response | 
|  | 53 | + | 
|  | 54 | +        return _wrapper | 
|  | 55 | + | 
|  | 56 | +    return decorator | 
0 commit comments