Skip to main content

Aiohttp-cache

aiohttp-cache logo

What's aiohttp-cache

aiohttp-cache is a plugin for aiohttp.web server that allow to use a cache system to improve the performance of your site.

How to use it

With in-memory backend

import asyncio

from aiohttp import web

from aiohttp_cache import (  # noqa
    setup_cache,
    cache,
)

PAYLOAD = {"hello": "aiohttp_cache"}
WAIT_TIME = 2


@cache()
async def some_long_running_view(
    request: web.Request,
) -> web.Response:
    await asyncio.sleep(WAIT_TIME)
    payload = await request.json()
    return web.json_response(payload)


app = web.Application()
setup_cache(app)
app.router.add_post("/", some_long_running_view)

web.run_app(app)

With redis backend

Note: redis should be available at $CACHE_URL env variable orredis://localhost:6379/0

import asyncio

import yarl
from aiohttp import web
from envparse import env

from aiohttp_cache import (  # noqa
    setup_cache,
    cache,
    RedisConfig,
)

PAYLOAD = {"hello": "aiohttp_cache"}
WAIT_TIME = 2


@cache()
async def some_long_running_view(
    request: web.Request,
) -> web.Response:
    await asyncio.sleep(WAIT_TIME)
    payload = await request.json()
    return web.json_response(payload)


app = web.Application()
url = yarl.URL(
    env.str("CACHE_URL", default="redis://localhost:6379/0")
)
setup_cache(
    app,
    cache_type="redis",
    backend_config=RedisConfig(
        db=int(url.path[1:]), host=url.host, port=url.port
    ),
)

app.router.add_post("/", some_long_running_view)

web.run_app(app)

Example with a custom cache key

Let's say you would like to cache the requests just by the method and json payload, then you can setup this as per the follwing example.

Note default key_pattern is:

DEFAULT_KEY_PATTERN = (
    AvailableKeys.method,
    AvailableKeys.host,
    AvailableKeys.path,
    AvailableKeys.postdata,
    AvailableKeys.ctype,
)
import asyncio

from aiohttp import web

from aiohttp_cache import (
    setup_cache,
    cache,
    AvailableKeys,
)  # noqa

PAYLOAD = {"hello": "aiohttp_cache"}
WAIT_TIME = 2


@cache()
async def some_long_running_view(
    request: web.Request,
) -> web.Response:
    await asyncio.sleep(WAIT_TIME)
    payload = await request.json()
    return web.json_response(payload)


custom_cache_key = (AvailableKeys.method, AvailableKeys.json)

app = web.Application()
setup_cache(app, key_pattern=custom_cache_key)
app.router.add_post("/", some_long_running_view)

web.run_app(app)

Parametrize the cache decorator

import asyncio

from aiohttp import web

from aiohttp_cache import (  # noqa
    setup_cache,
    cache,
)

PAYLOAD = {"hello": "aiohttp_cache"}
WAIT_TIME = 2


@cache(
    expires=1 * 24 * 3600,  # in seconds
    unless=False,  # anything what returns a bool. if True - skips cache
)
async def some_long_running_view(
    request: web.Request,
) -> web.Response:
    await asyncio.sleep(WAIT_TIME)
    payload = await request.json()
    return web.json_response(payload)


app = web.Application()
setup_cache(app)
app.router.add_post("/", some_long_running_view)

web.run_app(app)

License

This project is released under BSD license. Feel free

Source Code

The latest developer version is available in a github repository: https://github.com/cr0hn/aiohttp-cache

Development environment

  1. docker-compose run tests

Metadata

Release files for aiohttp-cache 4.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp-cache 4.0.1
File Size Uploaded Provenance
aiohttp_cache-4.0.1.tar.gz 8.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-cache 4.0.1
File Interpreter ABI Platform
aiohttp_cache-4.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / aiohttp_cache-4.0.1.tar.gz

Download URL aiohttp_cache-4.0.1.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5cab303a194f7bf2bf84ec9de1186954bbcb08c7e08cded56d7ef2eaa6e8d8e1
BLAKE2b-256 checksum
How to use checksums
47ef9e84ebb7236e833218d20a83cc598af2844ffb2aeb8b38fe88e7b64b78d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.6 Darwin/22.3.0

Release files / aiohttp_cache-4.0.1-py3-none-any.whl

Download URL aiohttp_cache-4.0.1-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a895b25fc9dd59b71162e0f11999a2a5ebe1862e731dd2d89314ccfd7437fceb
BLAKE2b-256 checksum
How to use checksums
84b3149af1f24153984fc621440e18d481212643a759e9fd7d2d1cf222948182
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.6 Darwin/22.3.0

Security for aiohttp-cache 4.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page