include aioredis and proper cache because I forgor

This commit is contained in:
Alexandre Teles 2022-08-31 21:14:49 -03:00
parent 3e484ddee6
commit 228c75c662
4 changed files with 53 additions and 1 deletions

17
main.py
View File

@ -9,6 +9,9 @@ import modules.ResponseModels as ResponseModels
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache
"""Get latest ReVanced releases from GitHub API."""
@ -35,6 +38,12 @@ limiter = Limiter(key_func=get_remote_address, headers_enabled=True)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# Setup cache
@cache()
async def get_cache():
return 1
# Routes
@app.get("/", response_class=RedirectResponse, status_code=301)
@ -49,6 +58,7 @@ async def root(request: Request, response: Response) -> RedirectResponse:
@app.get('/tools', response_model=ResponseModels.ToolsResponseModel)
@limiter.limit(config['slowapi']['limit'])
@cache(expire=60)
async def tools(request: Request, response: Response) -> dict:
"""Get patching tools' latest version.
@ -59,6 +69,7 @@ async def tools(request: Request, response: Response) -> dict:
@app.get('/apps', response_model=ResponseModels.AppsResponseModel)
@limiter.limit(config['slowapi']['limit'])
@cache(expire=60)
async def apps(request: Request, response: Response) -> dict:
"""Get patchable apps.
@ -69,6 +80,7 @@ async def apps(request: Request, response: Response) -> dict:
@app.get('/patches', response_model=ResponseModels.PatchesResponseModel)
@limiter.limit(config['slowapi']['limit'])
@cache(expire=60)
async def patches(request: Request, response: Response) -> dict:
"""Get latest patches.
@ -78,6 +90,11 @@ async def patches(request: Request, response: Response) -> dict:
return await releases.get_patches_json()
@app.on_event("startup")
async def startup():
redis = aioredis.from_url("redis://localhost", encoding="utf8", decode_responses=True)
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
# Run app
if __name__ == '__main__':
uvicorn.run(app, host=config['uvicorn']['host'], port=config['uvicorn']['port'])

33
poetry.lock generated
View File

@ -1,3 +1,18 @@
[[package]]
name = "aioredis"
version = "2.0.1"
description = "asyncio (PEP 3156) Redis support"
category = "main"
optional = false
python-versions = ">=3.6"
[package.dependencies]
async-timeout = "*"
typing-extensions = "*"
[package.extras]
hiredis = ["hiredis (>=1.0)"]
[[package]]
name = "aiorwlock"
version = "1.3.0"
@ -23,6 +38,14 @@ doc = ["packaging", "sphinx-autodoc-typehints (>=1.2.0)", "sphinx-rtd-theme"]
test = ["contextlib2", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (<0.15)", "uvloop (>=0.15)"]
trio = ["trio (>=0.16)"]
[[package]]
name = "async-timeout"
version = "4.0.2"
description = "Timeout context manager for asyncio programs"
category = "main"
optional = false
python-versions = ">=3.6"
[[package]]
name = "attrs"
version = "21.4.0"
@ -473,9 +496,13 @@ python-versions = ">=3.7"
[metadata]
lock-version = "1.1"
python-versions = "^3.10"
content-hash = "c776dc014863c1954d483fdb28452f979616522dfafab4f452976a82fa76e296"
content-hash = "3341dd17090ab80593c3d3c8db8710deb8456676623eca0144f27ffe45207307"
[metadata.files]
aioredis = [
{file = "aioredis-2.0.1-py3-none-any.whl", hash = "sha256:9ac0d0b3b485d293b8ca1987e6de8658d7dafcca1cddfcd1d506cae8cdebfdd6"},
{file = "aioredis-2.0.1.tar.gz", hash = "sha256:eaa51aaf993f2d71f54b70527c440437ba65340588afeb786cd87c55c89cd98e"},
]
aiorwlock = [
{file = "aiorwlock-1.3.0-py3-none-any.whl", hash = "sha256:45baf8e4fa9a23e0bb325fbd67da80de1fd7ae1d4f59a6381754c60cec7b289b"},
{file = "aiorwlock-1.3.0.tar.gz", hash = "sha256:83f12d87df4b9728a0b8fda1756585ab0d652b107bab59c6084e1b1ad692ab45"},
@ -484,6 +511,10 @@ anyio = [
{file = "anyio-3.6.1-py3-none-any.whl", hash = "sha256:cb29b9c70620506a9a8f87a309591713446953302d7d995344d0d7c6c0c9a7be"},
{file = "anyio-3.6.1.tar.gz", hash = "sha256:413adf95f93886e442aea925f3ee43baa5a765a64a0f52c6081894f9992fdd0b"},
]
async-timeout = [
{file = "async-timeout-4.0.2.tar.gz", hash = "sha256:2163e1640ddb52b7a8c80d0a67a08587e5d245cc9c553a74a847056bc2976b15"},
{file = "async_timeout-4.0.2-py3-none-any.whl", hash = "sha256:8ca1e4fcf50d07413d66d1a5e416e42cfdf5851c981d679a09851a6853383b3c"},
]
attrs = [
{file = "attrs-21.4.0-py2.py3-none-any.whl", hash = "sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4"},
{file = "attrs-21.4.0.tar.gz", hash = "sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd"},

View File

@ -15,6 +15,7 @@ toml = ">=0.10.2"
slowapi = ">=0.1.6"
orjson = ">=3.8.0"
fastapi-cache2 = ">=0.1.9"
aioredis = ">=2.0.1"
[tool.poetry.dev-dependencies]
mypy = ">=0.971"
@ -26,6 +27,7 @@ toml = ">=0.10.2"
slowapi = ">=0.1.6"
orjson = ">=3.8.0"
fastapi-cache2 = ">=0.1.9"
aioredis = ">=2.0.1"
[build-system]
requires = ["poetry-core>=1.0.0"]

View File

@ -1,5 +1,7 @@
aioredis==2.0.1; python_version >= "3.6"
aiorwlock==1.3.0; python_version >= "3.7" and python_version < "4.0" and python_full_version >= "3.7.0"
anyio==3.6.1; python_version >= "3.7" and python_version < "4.0" and python_full_version >= "3.6.2"
async-timeout==4.0.2; python_version >= "3.6"
attrs==21.4.0; python_version >= "3.7" and python_full_version < "3.0.0" and python_version < "4.0" or python_version >= "3.7" and python_version < "4.0" and python_full_version >= "3.5.0"
certifi==2022.6.15; python_version >= "3.7" and python_version < "4.0"
click==8.1.3; python_version >= "3.7" and python_version < "4.0"