mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
feat: merge master
This commit is contained in:
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@@ -4,11 +4,11 @@ jobs:
|
|||||||
ci:
|
ci:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-python@v2
|
- uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
python-version: "3.x"
|
python-version: "3.x"
|
||||||
- uses: abatilo/actions-poetry@v2.1.3
|
- uses: abatilo/actions-poetry@v2.1.6
|
||||||
- name: Config poetry
|
- name: Config poetry
|
||||||
run: poetry config experimental.new-installer false
|
run: poetry config experimental.new-installer false
|
||||||
- name: CI
|
- name: CI
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ import pendulum
|
|||||||
import redis.asyncio as redis
|
import redis.asyncio as redis
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from redis.asyncio.connection import ConnectionPool
|
|
||||||
from starlette.requests import Request
|
|
||||||
from starlette.responses import Response, JSONResponse
|
from starlette.responses import Response, JSONResponse
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
|
from redis.asyncio.connection import ConnectionPool
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import Response
|
||||||
|
|
||||||
from fastapi_cache import FastAPICache
|
from fastapi_cache import FastAPICache
|
||||||
from fastapi_cache.backends.redis import RedisBackend
|
from fastapi_cache.backends.redis import RedisBackend
|
||||||
from fastapi_cache.coder import PickleCoder
|
from fastapi_cache.coder import PickleCoder
|
||||||
@@ -18,10 +20,11 @@ from fastapi_cache.decorator import cache
|
|||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
app.mount(
|
app.mount(
|
||||||
path='/static',
|
path="/static",
|
||||||
app=StaticFiles(directory='./'), name='static',
|
app=StaticFiles(directory="./"),
|
||||||
|
name="static",
|
||||||
)
|
)
|
||||||
templates = Jinja2Templates(directory='./')
|
templates = Jinja2Templates(directory="./")
|
||||||
ret = 0
|
ret = 0
|
||||||
|
|
||||||
|
|
||||||
@@ -65,12 +68,10 @@ async def get_datetime(request: Request, response: Response):
|
|||||||
return pendulum.now()
|
return pendulum.now()
|
||||||
|
|
||||||
|
|
||||||
@app.get('/html', response_class=HTMLResponse)
|
@app.get("/html", response_class=HTMLResponse)
|
||||||
@cache(expire=60, namespace="html", coder=PickleCoder)
|
@cache(expire=60, namespace="html", coder=PickleCoder)
|
||||||
async def cache_html(request: Request):
|
async def cache_html(request: Request):
|
||||||
return templates.TemplateResponse('index.html', {
|
return templates.TemplateResponse("index.html", {"request": request, "ret": await get_ret()})
|
||||||
'request': request, "ret": await get_ret()
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/cache_response_obj")
|
@app.get("/cache_response_obj")
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import abc
|
import abc
|
||||||
from typing import Tuple, Optional
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
|
|
||||||
class Backend:
|
class Backend:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from typing import Tuple, Optional
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from aiobotocore.client import AioBaseClient
|
from aiobotocore.client import AioBaseClient
|
||||||
from aiobotocore.session import get_session
|
from aiobotocore.session import get_session
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from typing import Tuple, Optional
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from aiomcache import Client
|
from aiomcache import Client
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from typing import Tuple, Optional
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from redis.asyncio.client import Redis
|
from redis.asyncio.client import Redis
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import datetime
|
|||||||
import json
|
import json
|
||||||
import pickle # nosec:B403
|
import pickle # nosec:B403
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from typing import Any, Dict, Union
|
from typing import Any
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import sys
|
import sys
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Any, Awaitable, Callable, Optional, TypeVar, Type
|
from typing import Any, Awaitable, Callable, Optional, Type, TypeVar
|
||||||
|
|
||||||
if sys.version_info >= (3, 10):
|
if sys.version_info >= (3, 10):
|
||||||
from typing import ParamSpec
|
from typing import ParamSpec
|
||||||
@@ -15,7 +15,6 @@ from starlette.responses import Response
|
|||||||
from fastapi_cache import FastAPICache
|
from fastapi_cache import FastAPICache
|
||||||
from fastapi_cache.coder import Coder
|
from fastapi_cache.coder import Coder
|
||||||
|
|
||||||
|
|
||||||
P = ParamSpec("P")
|
P = ParamSpec("P")
|
||||||
R = TypeVar("R")
|
R = TypeVar("R")
|
||||||
|
|
||||||
@@ -113,7 +112,7 @@ def cache(
|
|||||||
request=request,
|
request=request,
|
||||||
response=response,
|
response=response,
|
||||||
args=args,
|
args=args,
|
||||||
kwargs=copy_kwargs
|
kwargs=copy_kwargs,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
cache_key = key_builder(
|
cache_key = key_builder(
|
||||||
@@ -122,7 +121,7 @@ def cache(
|
|||||||
request=request,
|
request=request,
|
||||||
response=response,
|
response=response,
|
||||||
args=args,
|
args=args,
|
||||||
kwargs=copy_kwargs
|
kwargs=copy_kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
ttl, ret = await backend.get_with_ttl(cache_key)
|
ttl, ret = await backend.get_with_ttl(cache_key)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
from typing import Optional, Callable
|
from typing import Callable, Optional
|
||||||
|
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import Response
|
from starlette.responses import Response
|
||||||
|
|||||||
743
poetry.lock
generated
743
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -23,6 +23,7 @@ aiomcache = { version = "*", optional = true }
|
|||||||
pendulum = "*"
|
pendulum = "*"
|
||||||
aiobotocore = { version = "^1.4.1", optional = true }
|
aiobotocore = { version = "^1.4.1", optional = true }
|
||||||
typing-extensions = { version = ">=4.1.0", markers = "python_version < \"3.10\"" }
|
typing-extensions = { version = ">=4.1.0", markers = "python_version < \"3.10\"" }
|
||||||
|
aiohttp= { version = ">=3.8.3", markers = "python_version >= \"3.11\""}
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
flake8 = "*"
|
flake8 = "*"
|
||||||
|
|||||||
@@ -3,11 +3,10 @@ from typing import Generator
|
|||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from fastapi_cache import FastAPICache
|
|
||||||
from starlette.testclient import TestClient
|
from starlette.testclient import TestClient
|
||||||
|
|
||||||
from examples.in_memory.main import app
|
from examples.in_memory.main import app
|
||||||
|
from fastapi_cache import FastAPICache
|
||||||
from fastapi_cache.backends.inmemory import InMemoryBackend
|
from fastapi_cache.backends.inmemory import InMemoryBackend
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user