mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Fix default json coder for datetime.
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
## 0.1
|
## 0.1
|
||||||
|
|
||||||
|
### 0.1.7
|
||||||
|
|
||||||
|
- Fix default json coder for datetime.
|
||||||
|
|
||||||
### 0.1.6
|
### 0.1.6
|
||||||
|
|
||||||
- Fix redis cache.
|
- Fix redis cache.
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from datetime import date, datetime
|
||||||
|
|
||||||
import aioredis
|
import aioredis
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
@@ -31,6 +33,18 @@ async def clear():
|
|||||||
return await FastAPICache.clear(namespace="test")
|
return await FastAPICache.clear(namespace="test")
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/date")
|
||||||
|
@cache(namespace="test", expire=20)
|
||||||
|
async def get_data(request: Request, response: Response):
|
||||||
|
return date.today()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/datetime")
|
||||||
|
@cache(namespace="test", expire=20)
|
||||||
|
async def get_datetime(request: Request, response: Response):
|
||||||
|
return datetime.now()
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def startup():
|
async def startup():
|
||||||
redis = aioredis.from_url(url="redis://localhost")
|
redis = aioredis.from_url(url="redis://localhost")
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import pendulum
|
|||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
|
|
||||||
CONVERTERS = {
|
CONVERTERS = {
|
||||||
"date": pendulum.parse,
|
"date": lambda x: pendulum.parse(x, exact=True),
|
||||||
"datetime": lambda x: pendulum.parse(x, exact=True),
|
"datetime": lambda x: pendulum.parse(x, exact=True),
|
||||||
"decimal": Decimal,
|
"decimal": Decimal,
|
||||||
}
|
}
|
||||||
@@ -17,12 +17,9 @@ CONVERTERS = {
|
|||||||
class JsonEncoder(json.JSONEncoder):
|
class JsonEncoder(json.JSONEncoder):
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
if isinstance(obj, datetime.datetime):
|
if isinstance(obj, datetime.datetime):
|
||||||
if obj.tzinfo:
|
return {"val": str(obj), "_spec_type": "datetime"}
|
||||||
return {"val": obj.strftime("%Y-%m-%d %H:%M:%S%z"), "_spec_type": "datetime"}
|
|
||||||
else:
|
|
||||||
return {"val": obj.strftime("%Y-%m-%d %H:%M:%S"), "_spec_type": "datetime"}
|
|
||||||
elif isinstance(obj, datetime.date):
|
elif isinstance(obj, datetime.date):
|
||||||
return {"val": obj.strftime("%Y-%m-%d"), "_spec_type": "date"}
|
return {"val": str(obj), "_spec_type": "date"}
|
||||||
elif isinstance(obj, Decimal):
|
elif isinstance(obj, Decimal):
|
||||||
return {"val": str(obj), "_spec_type": "decimal"}
|
return {"val": str(obj), "_spec_type": "decimal"}
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "fastapi-cache2"
|
name = "fastapi-cache2"
|
||||||
version = "0.1.6"
|
version = "0.1.7"
|
||||||
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