Merge pull request #108 from hackjammer/master

Transparent passthrough in the event of cache backend connection issues
This commit is contained in:
long2ice
2023-01-11 10:45:11 +08:00
committed by GitHub
2 changed files with 13 additions and 4 deletions

View File

@@ -5,6 +5,7 @@
### 0.2.1
- Support cache jinja2 template response.
- Transparently handle backend connection failures.
### 0.2.0

View File

@@ -123,13 +123,18 @@ def cache(
args=args,
kwargs=copy_kwargs,
)
try:
ttl, ret = await backend.get_with_ttl(cache_key)
except ConnectionError:
ttl, ret = 0, None
if not request:
if ret is not None:
return coder.decode(ret)
ret = await ensure_async_func(*args, **kwargs)
try:
await backend.set(cache_key, coder.encode(ret), expire or FastAPICache.get_expire())
except ConnectionError:
pass
return ret
if request.method != "GET":
@@ -148,7 +153,10 @@ def cache(
ret = await ensure_async_func(*args, **kwargs)
try:
await backend.set(cache_key, coder.encode(ret), expire or FastAPICache.get_expire())
except ConnectionError:
pass
return ret
return inner