onecache is a Python library providing in-memory caching for both synchronous and asynchronous code. It implements an LRU (Least Recently Used) algorithm and supports time-to-live (TTL) expiration for cache entries. The library is currently at version 0.8.1, with its latest update on February 20, 2026, indicating active maintenance though releases might be infrequent.
pip install onecacheVerified import paths — ran on the pinned version, not inferred.
This example demonstrates basic usage of `CacheDecorator` for synchronous functions and `AsyncCacheDecorator` for asynchronous functions. It shows how to apply the decorators with `maxsize` and `ttl` parameters, and how cache hits and misses affect the underlying function's execution count. For the async example, `asyncio.run()` is used to execute the main coroutine.
Avoid relying on `max_mem_size` for memory control when deploying to PyPy. Implement external memory monitoring or alternative eviction strategies if strict memory limits are required in PyPy.
For thread-safe operation in concurrent environments, explicitly set `thread_safe=True` in the decorator arguments: `@CacheDecorator(thread_safe=True)` or `@AsyncCacheDecorator(thread_safe=True)`. This will use a lock to protect cache access.
If you want the TTL to be reset (refreshed) whenever a cached item is accessed, set the `refresh_ttl` parameter to `True` in the decorator: `@CacheDecorator(ttl=60000, refresh_ttl=True)`.
For persistent caching across application restarts, integrate `onecache` with a separate persistent storage layer (e.g., Redis, database, file system) or choose a different caching library designed for persistence (e.g., `requests-cache` for HTTP, `anycache` for general object persistence).
pip install onecache
from onecache import cache
from onecache import cache
@cache(ttl=60, maxsize=100)
def my_func():
return 'cached data'from onecache import cache
@cache(key_builder=lambda *args, **kwargs: f'custom_key_{args[0]}')
def my_func(arg1, arg2='default'):
return arg1 + arg2No dependency data recorded yet.