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