Python cache for sync and async code
Project description
OneCache
Python cache for sync and async code.
Cache uses LRU algoritm. Cache can optionally have TTL.
Tested in python 3.7, 3.9, 3.11 and pypy3.9 for windows, mac and linux (see github status badge), it should work in versions between them. It may work for python3.6
Usage
from onecache import CacheDecorator
from onecache import AsyncCacheDecorator
class Counter:
def __init__(self, count=0):
self.count = count
@pytest.mark.asyncio
async def test_async_cache_counter():
"""Test async cache, counter case."""
counter = Counter()
@AsyncCacheDecorator()
async def mycoro(counter: Counter):
counter.count += 1
return counter.count
assert 1 == (await mycoro(counter))
assert 1 == (await mycoro(counter))
def test_cache_counter():
"""Test async cache, counter case."""
counter = Counter()
@CacheDecorator()
def sample(counter: Counter):
counter.count += 1
return counter.count
assert 1 == (sample(counter))
assert 1 == (sample(counter))
Decorator classes supports the following arguments
- maxsize (int): Maximun number of items to be cached. default: 512
- ttl (int): time to expire in milliseconds, if None, it does not expire. default: None
- skip_args (bool): apply cache as the function doesn't have any arguments, default: False
- cache_class (class): Class to use for cache instance. default: LRUCache
- refresh_ttl (bool): if cache with ttl, This flag makes key expiration timestamp to be refresh per access. default: False
- thread_safe (bool): tell decorator to use thread safe lock. default=False
- max_mem_size (int): max mem size in bytes. Ceil for sum of cache values sizes. default=None which means no limit. For pypy this value is ignored as the objects can change by the JIT compilation.
If num of records exceds maxsize, it drops the oldest.
Development
Install packages with pip-tools:
pip install pip-tools
pip-compile
pip-compile test-requirements.in
pip-sync requirements.txt test-requirements.txt
Contribute
- Fork
- create a branch
feature/your_feature
- commit - push - pull request
Thanks :)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
onecache-0.7.0.tar.gz
(4.3 kB
view details)
Built Distribution
File details
Details for the file onecache-0.7.0.tar.gz
.
File metadata
- Download URL: onecache-0.7.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7cb3b4f8d63d0c6153cc3174b11f70cd1ae38c06ed57c6972b9f3fce0b4945f |
|
MD5 | 7df977480ca1cbce0855806e67b52b0b |
|
BLAKE2b-256 | 984700238b2fcdd33be53c1e28f2686d56bf967db08e9bec826a8c15a156dad6 |
File details
Details for the file onecache-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: onecache-0.7.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b39e575f7d75791d826e65c609fa1d718a6ad7425ec1a440f7448214ca4de528 |
|
MD5 | db775a2b9e1879ba7d76e25595874467 |
|
BLAKE2b-256 | 7abdcb49230c14e52cb895f7daf116f7744d434e3fef65fb2975cccc2f3d34b0 |