mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Merge pull request #130 from mjpieters/non_get_requests
Fix handling non-GET requests
This commit is contained in:
@@ -95,6 +95,17 @@ async def pydantic_instance() -> Item:
|
|||||||
return Item(name="Something", description="An instance of a Pydantic model", price=10.5)
|
return Item(name="Something", description="An instance of a Pydantic model", price=10.5)
|
||||||
|
|
||||||
|
|
||||||
|
put_ret = 0
|
||||||
|
|
||||||
|
|
||||||
|
@app.put("/uncached_put")
|
||||||
|
@cache(namespace="test", expire=5)
|
||||||
|
async def uncached_put():
|
||||||
|
global put_ret
|
||||||
|
put_ret = put_ret + 1
|
||||||
|
return {"value": put_ret}
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def startup():
|
async def startup():
|
||||||
FastAPICache.init(InMemoryBackend())
|
FastAPICache.init(InMemoryBackend())
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ def cache(
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
if request.method != "GET":
|
if request.method != "GET":
|
||||||
return await ensure_async_func(request, *args, **kwargs)
|
return await ensure_async_func(*args, **kwargs)
|
||||||
|
|
||||||
if_none_match = request.headers.get("if-none-match")
|
if_none_match = request.headers.get("if-none-match")
|
||||||
if ret is not None:
|
if ret is not None:
|
||||||
|
|||||||
@@ -86,3 +86,11 @@ def test_pydantic_model() -> None:
|
|||||||
r1 = client.get("/pydantic_instance").json()
|
r1 = client.get("/pydantic_instance").json()
|
||||||
r2 = client.get("/pydantic_instance").json()
|
r2 = client.get("/pydantic_instance").json()
|
||||||
assert r1 == r2
|
assert r1 == r2
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_get() -> None:
|
||||||
|
with TestClient(app) as client:
|
||||||
|
response = client.put("/uncached_put")
|
||||||
|
assert response.json() == {"value": 1}
|
||||||
|
response = client.put("/uncached_put")
|
||||||
|
assert response.json() == {"value": 2}
|
||||||
|
|||||||
Reference in New Issue
Block a user