Skip to main content

PyPI License

Async CacheControl for aiohttp

Requires Python3.6+

Note: Library is still under development, there might be a lot of bugs.

For contributing see development_notes.md as a starting guide

What and why

There is a good and simple library CacheControl written for python requests library. And there is nothing similar for aiohttp. "Async CacheControl" project strives to cover this hole.

Usage

import asyncio
from acachecontrol import AsyncCache, AsyncCacheControl


async def main():
    cache = AsyncCache(config={"sleep_time": 0.2})
    # `AsyncCache()` with default configuration is used
    # if `cache` not provided
    async with AsyncCacheControl(cache=cache) as cached_sess:
        async with cached_sess.get('http://example.com') as resp:
            resp_text = await resp.text()
            print(resp_text)


asyncio.run(main())

Extending or creating new classes

It is possible to use any cache backend, which should implement OrderedDict interfaces: __contains__, __len__, __getitem__, __setitem__, get, pop, popitem, move_to_end:

class CustomCacheBackend():
    def __init__(self):
        self.item_order = []
        self.storage = {}

    def __contains__(self, key):
        return key in self.storage

    def __len__(self):
        return len(self.storage)

    def __getitem__(self, key):
        return self.storage[key]

    def __setitem__(self, key, value):
        self.storage[key] = value
        self.item_order.append(key)

    def get(self, key):
        return self.storage.get(key)

    def pop(self, key):
        self.item_order.remove(key)
        return self.storage.pop(key)

    def move_to_end(self, key):
        last_index = len(self.item_order) - 1
        key_index = self.item_order.index(key)
        while key_index < last_index:
            self.item_order[key_index] = self.item_order[key_index+1]
            key_index += 1
        self.item_order[last_index] = key

    def popitem(self, last=True):
        key = self.item_order.pop() if last else self.item_order.pop(0)
        value = self.storage.pop(key)
        return value

Then you can use it in AsyncCache:

import asyncio
from acachecontrol import AsyncCache, AsyncCacheControl


async def main():
    cache = AsyncCache(cache_backend=CustomCacheBackend())
    async with AsyncCacheControl(cache=cache) as cached_sess:
        async with cached_sess.get('http://example.com') as resp:
            resp_text = await resp.text()
            print(resp_text)


asyncio.run(main())

Similarly, you can replace RequestContextManager (assume its implementation in module custom_implementations):

import asyncio
from acachecontrol import AsyncCache, AsyncCacheControl

from custom_implementations import CustomRequestContextManager


async def main():
    async with AsyncCacheControl(request_context_manager_cls=CustomRequestContextManager) as cached_sess:
        async with cached_sess.get('http://example.com') as resp:
            resp_text = await resp.text()
            print(resp_text)


asyncio.run(main())

Metadata

Release files for acachecontrol 0.3.7

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

Source distribution (sdist)

Source distribution for acachecontrol 0.3.7
File Size Uploaded
acachecontrol-0.3.7.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for acachecontrol 0.3.7
File Interpreter ABI Platform
acachecontrol-0.3.7-py3-none-any.whl Python 3 none any Details

Total release size: 16.8 kB

Release files / acachecontrol-0.3.7.tar.gz

Download URL acachecontrol-0.3.7.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3331ebf0a4299055879a0c378d9d82f2484bb9da34f933b4816705abb17c25bc
BLAKE2b-256 checksum
How to use checksums
45eb131a4d14ef35234809537262415004868471fe57abce228253cb3f815e65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / acachecontrol-0.3.7-py3-none-any.whl

Download URL acachecontrol-0.3.7-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
510c6cb3140d371dbeaed831b4488ddb631d0abe263d2fc5bae9e78d926a32ac
BLAKE2b-256 checksum
How to use checksums
c2418409544483fac9b8dcabac46b371e655f836223831e17267f1e6e2a64379
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.3.7 This release

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

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