Add logging to decorator.py on backend failures

This commit is contained in:
hackjammer
2023-01-17 12:15:53 +00:00
parent e8193b5c22
commit ea1ffcd7b4
2 changed files with 8 additions and 3 deletions

View File

@@ -4,8 +4,9 @@
### 0.2.1 ### 0.2.1
- Fix picklecoder - Fix picklecoder
- Fix connection failure transparency - Fix connection failure transparency and add logging
- Add Cache-Control and ETag on first response - Add Cache-Control and ETag on first response
- Support Async RedisCluster client from redis-py
### 0.2.0 ### 0.2.0

View File

@@ -2,6 +2,7 @@ import inspect
import sys import sys
from functools import wraps from functools import wraps
from typing import Any, Awaitable, Callable, Optional, Type, TypeVar from typing import Any, Awaitable, Callable, Optional, Type, TypeVar
import logging
if sys.version_info >= (3, 10): if sys.version_info >= (3, 10):
from typing import ParamSpec from typing import ParamSpec
@@ -15,6 +16,8 @@ from starlette.responses import Response
from fastapi_cache import FastAPICache from fastapi_cache import FastAPICache
from fastapi_cache.coder import Coder from fastapi_cache.coder import Coder
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
P = ParamSpec("P") P = ParamSpec("P")
R = TypeVar("R") R = TypeVar("R")
@@ -126,6 +129,7 @@ def cache(
try: try:
ttl, ret = await backend.get_with_ttl(cache_key) ttl, ret = await backend.get_with_ttl(cache_key)
except Exception: except Exception:
logger.warning(f"Error retrieving cache key '{cache_key}' from backend:", exc_info=True)
ttl, ret = 0, None ttl, ret = 0, None
if not request: if not request:
if ret is not None: if ret is not None:
@@ -134,7 +138,7 @@ def cache(
try: try:
await backend.set(cache_key, coder.encode(ret), expire) await backend.set(cache_key, coder.encode(ret), expire)
except Exception: except Exception:
pass logger.warning(f"Error setting cache key '{cache_key}' in backend:", exc_info=True)
return ret return ret
if request.method != "GET": if request.method != "GET":
@@ -157,7 +161,7 @@ def cache(
try: try:
await backend.set(cache_key, encoded_ret, expire) await backend.set(cache_key, encoded_ret, expire)
except Exception: except Exception:
pass logger.warning(f"Error setting cache key '{cache_key}' in backend:", exc_info=True)
response.headers["Cache-Control"] = f"max-age={expire}" response.headers["Cache-Control"] = f"max-age={expire}"
etag = f"W/{hash(encoded_ret)}" etag = f"W/{hash(encoded_ret)}"