Caching middleware for aiohttp server with aiocache under the hood. Inspired by aiohttp-cache.
pip install aiohttp-aiocacheor
poetry add aiohttp-aiocacheOptional aiocache dependencies for redis, memcached and msgpack support
will not be installed. Install them manually if required.
import asyncio
import aiohttp.web as web
from aiocache import Cache
from aiocache.serializers import PickleSerializer
from aiohttp_aiocache import cached, register_cache
@cached # mark handler with decorator
async def handler(_: web.Request) -> web.Response:
await asyncio.sleep(1)
return web.Response(text="Hello world")
app = web.Application()
app.router.add_route("GET", "/", handler)
# create aiocache instance
cache = Cache(
Cache.MEMORY,
serializer=PickleSerializer(),
namespace="main",
ttl=60,
)
# register cache backend in appplication
register_cache(app, cache)
web.run_app(app)Support caching for GET requests only.
MIT