Skip to main content

A cache system for aiohttp server

Project description

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)

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-cache-2.0.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_cache-2.0.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-cache-2.0.1.tar.gz.

File metadata

  • Download URL: aiohttp-cache-2.0.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.8.1 Linux/4.19.78-coreos

File hashes

Hashes for aiohttp-cache-2.0.1.tar.gz
Algorithm Hash digest
SHA256 98ca2628f659383ef235e75dff5697e0ae4bcf00adc6482bd8d495022972c124
MD5 1a2fb8e2a2092843018a6ff7a5afe471
BLAKE2b-256 44436d6739220ed975c8f387ac4a64fc03d75ba422286659818188937e02c077

See more details on using hashes here.

File details

Details for the file aiohttp_cache-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_cache-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.8.1 Linux/4.19.78-coreos

File hashes

Hashes for aiohttp_cache-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c90fa7ef6357fd2e00d725383cd15058de378be98c158d88e788d5429d8aee8a
MD5 3d7b7badb33ddec1699972029732d2c4
BLAKE2b-256 14039b566e4954634026082c428bf716e0b17f721377806e771a1db6a8b0949c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page