mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
- Fix redis cache.
- Encode key builder.
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
|
||||
## 0.1
|
||||
|
||||
### 0.1.6
|
||||
|
||||
- Fix redis cache.
|
||||
- Encode key builder.
|
||||
|
||||
### 0.1.5
|
||||
|
||||
- Fix setting expire for redis (#24)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import aioredis
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from starlette.requests import Request
|
||||
@@ -5,6 +6,7 @@ from starlette.responses import Response
|
||||
|
||||
from fastapi_cache import FastAPICache
|
||||
from fastapi_cache.backends.inmemory import InMemoryBackend
|
||||
from fastapi_cache.backends.redis import RedisBackend
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
app = FastAPI()
|
||||
@@ -20,7 +22,7 @@ async def get_ret():
|
||||
|
||||
|
||||
@app.get("/")
|
||||
@cache(namespace="test", expire=2)
|
||||
@cache(namespace="test", expire=20)
|
||||
async def index(request: Request, response: Response):
|
||||
return dict(ret=await get_ret())
|
||||
|
||||
@@ -32,7 +34,8 @@ async def clear():
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
|
||||
redis = aioredis.from_url(url="redis://localhost")
|
||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -10,10 +10,8 @@ class RedisBackend(Backend):
|
||||
self.redis = redis
|
||||
|
||||
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
|
||||
p = self.redis.pipeline()
|
||||
p.ttl(key)
|
||||
p.get(key)
|
||||
return await p.execute()
|
||||
async with self.redis.pipeline(transaction=True) as pipe:
|
||||
return await (pipe.ttl(key).get(key).execute())
|
||||
|
||||
async def get(self, key) -> str:
|
||||
return await self.redis.get(key)
|
||||
@@ -24,6 +22,6 @@ class RedisBackend(Backend):
|
||||
async def clear(self, namespace: str = None, key: str = None) -> int:
|
||||
if namespace:
|
||||
lua = f"for i, name in ipairs(redis.call('KEYS', '{namespace}:*')) do redis.call('DEL', name); end"
|
||||
return await self.redis.eval(lua)
|
||||
return await self.redis.eval(lua, numkeys=0)
|
||||
elif key:
|
||||
return await self.redis.delete(key)
|
||||
|
||||
@@ -3,9 +3,9 @@ import json
|
||||
import pickle # nosec:B403
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
|
||||
import dateutil.parser
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
|
||||
CONVERTERS = {
|
||||
"date": dateutil.parser.parse,
|
||||
|
||||
@@ -19,7 +19,7 @@ def default_key_builder(
|
||||
cache_key = (
|
||||
prefix
|
||||
+ hashlib.md5( # nosec:B303
|
||||
f"{func.__module__}:{func.__name__}:{args}:{kwargs}"
|
||||
f"{func.__module__}:{func.__name__}:{args}:{kwargs}".encode()
|
||||
).hexdigest()
|
||||
)
|
||||
return cache_key
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "fastapi-cache2"
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
description = "Cache for FastAPI"
|
||||
authors = ["long2ice <long2ice@gmail.com>"]
|
||||
license = "Apache-2.0"
|
||||
|
||||
Reference in New Issue
Block a user