Cache-Control for aiohttp
Project description
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())
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
Built Distribution
File details
Details for the file acachecontrol-0.3.5.tar.gz
.
File metadata
- Download URL: acachecontrol-0.3.5.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 143f5f88b131358f10457794b38f64d903adc2505b2db1ceb5a6a058339180c1 |
|
MD5 | fb67dd107910db188452331d9c577be9 |
|
BLAKE2b-256 | e6a25fe05c9f795d355e48686845ba293d259dba4453ca74d6a89812015c58a1 |
File details
Details for the file acachecontrol-0.3.5-py3-none-any.whl
.
File metadata
- Download URL: acachecontrol-0.3.5-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 901906a97b2a69b35a6f1a8530f934711acf6974ee763e411af152996e8657f7 |
|
MD5 | 73fa7453530b5397d17e204dfc580db8 |
|
BLAKE2b-256 | 92916fdd6ae50ef419434b7cfc2125f6bd5bca31967cc61fc5e4dcb3ece4cd62 |