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 an LRU algorithm and can optionally set TTLs per entry.

Tested automatically on CPython 3.8–3.14 and PyPy 3.9 across Linux, macOS, and Windows (see the workflow badge). Earlier versions may work but are not part of the supported matrix.

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 dependencies with Poetry (includes dev + test groups):

poetry install --with test,dev

Run the test suite and coverage locally:

poetry run pytest --cov

Lint and format checks:

poetry run flake8
poetry run autopep8 --in-place --recursive onecache tests

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.8.1.tar.gz (4.5 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.8.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for onecache-0.8.1.tar.gz
Algorithm Hash digest
SHA256 4df2d62ced1e6ed905e3965f8fb4bdb4c999ebb7fe044202c34b52a0034f3882
MD5 a8ab59b382d57b517d2d14a0200ed9d4
BLAKE2b-256 a756d442f06a3e0f2f45b2474afec2b3c1125aad325b93221911055dc55948a1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: onecache-0.8.1-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.12.3

File hashes

Hashes for onecache-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d0537ff473b571f39ebac8a8f11596390d8ef4370f957a376760dcc6f88a0fa
MD5 88b5ed470fba0980ce56a83296d27b16
BLAKE2b-256 d0a242832e60ecbe548978a2f08ff68c47ecc90e7af6a031866e091ffb07c0d1

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