Skip to main content

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 :)

Metadata

Release files for onecache 0.8.1

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

Source distribution (sdist)

Source distribution for onecache 0.8.1
File Size Uploaded
onecache-0.8.1.tar.gz 4.5 kB Details

Built distribution (wheel)

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

Total release size: 9.8 kB

Release files / onecache-0.8.1.tar.gz

Download URL onecache-0.8.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4df2d62ced1e6ed905e3965f8fb4bdb4c999ebb7fe044202c34b52a0034f3882
BLAKE2b-256 checksum
How to use checksums
a756d442f06a3e0f2f45b2474afec2b3c1125aad325b93221911055dc55948a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / onecache-0.8.1-py3-none-any.whl

Download URL onecache-0.8.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d0537ff473b571f39ebac8a8f11596390d8ef4370f957a376760dcc6f88a0fa
BLAKE2b-256 checksum
How to use checksums
d0a242832e60ecbe548978a2f08ff68c47ecc90e7af6a031866e091ffb07c0d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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