|
1 | | -from typing import Callable |
| 1 | +from typing import Callable, Union, List, Dict, Any |
2 | 2 | import functools |
| 3 | +import json |
| 4 | +from uuid import UUID |
| 5 | +from datetime import datetime |
3 | 6 |
|
4 | 7 | from fastapi import Request, Response |
5 | | -from fastapi.responses import JSONResponse |
6 | | -import redis.asyncio as redis |
7 | 8 | from redis.asyncio import Redis, ConnectionPool |
8 | | -from fastapi.responses import JSONResponse |
| 9 | +from sqlalchemy.orm import class_mapper, DeclarativeBase |
| 10 | + |
| 11 | +from app.core.exceptions import CacheIdentificationInferenceError |
9 | 12 |
|
10 | 13 | pool: ConnectionPool | None = None |
11 | 14 | client: Redis | None = None |
12 | 15 |
|
13 | | -def cache(key_prefix: str, expiration: int = 3600) -> Callable: |
| 16 | +def _serialize_sqlalchemy_object(obj: DeclarativeBase) -> Dict[str, Any]: |
| 17 | + """ |
| 18 | + Serialize a SQLAlchemy DeclarativeBase object to a dictionary. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + obj: DeclarativeBase |
| 23 | + The SQLAlchemy DeclarativeBase object to be serialized. |
| 24 | + |
| 25 | + Returns |
| 26 | + ------- |
| 27 | + Dict[str, Any] |
| 28 | + A dictionary containing the serialized attributes of the object. |
| 29 | + |
| 30 | + Note |
| 31 | + ---- |
| 32 | + - Datetime objects are converted to ISO 8601 string format. |
| 33 | + - UUID objects are converted to strings before serializing to JSON. |
| 34 | + """ |
| 35 | + if isinstance(obj, DeclarativeBase): |
| 36 | + data = {} |
| 37 | + for column in class_mapper(obj.__class__).columns: |
| 38 | + value = getattr(obj, column.name) |
| 39 | + |
| 40 | + if isinstance(value, datetime): |
| 41 | + value = value.isoformat() |
| 42 | + |
| 43 | + if isinstance(value, UUID): |
| 44 | + value = str(value) |
| 45 | + |
| 46 | + data[column.name] = value |
| 47 | + return data |
| 48 | + |
| 49 | + |
| 50 | +def _infer_resource_id(kwargs: Dict[str, Any], resource_id_type: Union[type, str]) -> Union[None, int, str]: |
| 51 | + """ |
| 52 | + Infer the resource ID from a dictionary of keyword arguments. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + kwargs: Dict[str, Any] |
| 57 | + A dictionary of keyword arguments. |
| 58 | + resource_id_type: Union[type, str] |
| 59 | + The expected type of the resource ID, which can be an integer (int) or a string (str). |
| 60 | + |
| 61 | + Returns |
| 62 | + ------- |
| 63 | + Union[None, int, str] |
| 64 | + The inferred resource ID. If it cannot be inferred or does not match the expected type, it returns None. |
| 65 | +
|
| 66 | + Note |
| 67 | + ---- |
| 68 | + - When `resource_id_type` is 'int', the function looks for an argument with the key 'id'. |
| 69 | + - When `resource_id_type` is 'str', it attempts to infer the resource ID as a string. |
| 70 | + """ |
| 71 | + resource_id = None |
| 72 | + for arg_name, arg_value in kwargs.items(): |
| 73 | + if isinstance(arg_value, resource_id_type): |
| 74 | + if (resource_id_type is int) and ("id" in arg_name): |
| 75 | + resource_id = arg_value |
| 76 | + |
| 77 | + elif (resource_id_type is int) and ("id" not in arg_name): |
| 78 | + pass |
| 79 | + |
| 80 | + elif resource_id_type is str: |
| 81 | + resource_id = arg_value |
| 82 | + |
| 83 | + if resource_id is None: |
| 84 | + raise CacheIdentificationInferenceError |
| 85 | + |
| 86 | + return resource_id |
| 87 | + |
| 88 | + |
| 89 | +def cache(key_prefix: str, resource_id_name: Any = None, expiration: int = 3600, resource_id_type: Union[type, List[type]] = int) -> Callable: |
| 90 | + """ |
| 91 | + Cache decorator for FastAPI endpoints. |
| 92 | +
|
| 93 | + This decorator allows you to cache the results of FastAPI endpoint functions, improving response times and reducing the load on the application by storing and retrieving data in a cache. |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + key_prefix: str |
| 98 | + A unique prefix to identify the cache key. |
| 99 | + resource_id: Any, optional |
| 100 | + The resource ID to be used in cache key generation. If not provided, it will be inferred from the endpoint's keyword arguments. |
| 101 | + expiration: int, optional |
| 102 | + The expiration time for cached data in seconds. Defaults to 3600 seconds (1 hour). |
| 103 | + resource_id_type: Union[type, List[type]], optional |
| 104 | + The expected type of the resource ID. This can be a single type (e.g., int) or a list of types (e.g., [int, str]). Defaults to int. |
| 105 | +
|
| 106 | + Returns |
| 107 | + ------- |
| 108 | + Callable |
| 109 | + A decorator function that can be applied to FastAPI endpoints. |
| 110 | +
|
| 111 | + Example usage |
| 112 | + ------------- |
| 113 | +
|
| 114 | + ```python |
| 115 | + from fastapi import FastAPI, Request |
| 116 | + from my_module import cache # Replace with your actual module and imports |
| 117 | +
|
| 118 | + app = FastAPI() |
| 119 | +
|
| 120 | + # Define a sample endpoint with caching |
| 121 | + @app.get("/sample/{resource_id}") |
| 122 | + @cache(key_prefix="sample_data", expiration=3600, resource_id_type=int) |
| 123 | + async def sample_endpoint(request: Request, resource_id: int): |
| 124 | + # Your endpoint logic here |
| 125 | + return {"data": "your_data"} |
| 126 | + ``` |
| 127 | +
|
| 128 | + This decorator caches the response data of the endpoint function using a unique cache key. |
| 129 | + The cached data is retrieved for GET requests, and the cache is invalidated for other types of requests. |
| 130 | +
|
| 131 | + Note: |
| 132 | + - For caching lists of objects, ensure that the response is a list of objects, and the decorator will handle caching accordingly. |
| 133 | + - resource_id_type is used only if resource_id is not passed. |
| 134 | + """ |
14 | 135 | def wrapper(func: Callable) -> Callable: |
15 | 136 | @functools.wraps(func) |
16 | 137 | async def inner(request: Request, *args, **kwargs) -> Response: |
17 | | - resource_id = args[0] # Assuming the resource ID is the first argument |
| 138 | + if resource_id_name: |
| 139 | + resource_id = kwargs[resource_id_name] |
| 140 | + else: |
| 141 | + resource_id = _infer_resource_id(kwargs=kwargs, resource_id_type=resource_id_type) |
| 142 | + |
18 | 143 | cache_key = f"{key_prefix}:{resource_id}" |
19 | 144 |
|
20 | 145 | if request.method == "GET": |
21 | | - # Check if the data exists in the cache for GET requests |
22 | 146 | cached_data = await client.get(cache_key) |
23 | 147 | if cached_data: |
24 | | - # If data exists in the cache, return it |
25 | | - return JSONResponse(content=cached_data.decode(), status_code=200) |
| 148 | + return json.loads(cached_data.decode()) |
26 | 149 |
|
27 | | - # Call the original function for both all types of requests |
28 | 150 | result = await func(request, *args, **kwargs) |
29 | | - |
| 151 | + |
30 | 152 | if request.method == "GET": |
31 | | - # Store the result in the cache for GET requests with the specified expiration time |
32 | | - await client.set(cache_key, result, expire=expiration) |
| 153 | + if isinstance(result, list): |
| 154 | + serialized_data = json.dumps( |
| 155 | + [_serialize_sqlalchemy_object(obj) for obj in result] |
| 156 | + ) |
| 157 | + else: |
| 158 | + serialized_data = json.dumps( |
| 159 | + _serialize_sqlalchemy_object(result) |
| 160 | + ) |
| 161 | + |
| 162 | + await client.set(cache_key, serialized_data) |
| 163 | + await client.expire(cache_key, expiration) |
33 | 164 | else: |
34 | | - # Invalidate the cache for other types of requests |
35 | | - await redis.delete(cache_key) |
| 165 | + await client.delete(cache_key) |
36 | 166 |
|
37 | | - return JSONResponse(content=result, status_code=200) |
| 167 | + return result |
38 | 168 |
|
39 | 169 | return inner |
40 | 170 |
|
|
0 commit comments