Skip to main content

Python cache for sync and async code

Project description

Coverage Status github status

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

  1. Fork
  2. create a branch feature/your_feature
  3. commit - push - pull request

Thanks :)

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

onecache-0.7.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

onecache-0.7.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file onecache-0.7.2.tar.gz.

File metadata

  • Download URL: onecache-0.7.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for onecache-0.7.2.tar.gz
Algorithm Hash digest
SHA256 e5942056e49b6ba41560c561f175bcc1881abaf2bb3bc96688d3e0196d2a5721
MD5 c7b74a387733e17f52e5e6ac6dd431ab
BLAKE2b-256 3d3dd03562e34add32312021f3af86239457086785c916030e68f72ddd766566

See more details on using hashes here.

File details

Details for the file onecache-0.7.2-py3-none-any.whl.

File metadata

  • Download URL: onecache-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for onecache-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49ff14b4f949ed09e9e34de3a40b3542414a52b88bd3eee18763d4058f8b8577
MD5 4eb7e76b3bdecff4e833f40e788f2b56
BLAKE2b-256 9d284881f26bfc47e83f89ffd9362418d5ba0518c3ba67025571f6e4b2e0fd70

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