A function-level memory cache that supports Time To Live (TTL)
Project description
TTL Cache
A function-level memory cache that supports Time To Live (TTL).
- Per-function argument caching: Caching is possible based on the data passed to function arguments. The cache key is composed of the function name, argument names, and the values of the arguments.
- Automatic expiration: Cached data expires and is automatically deleted after the specified TTL (in seconds).
- LRU policy: When the cache exceeds its maximum size (
max_size
), the Least Recently Used (LRU) policy is applied to delete items. - Easy application: Simply add the
@TtlCache(ttl=seconds)
decorator to the function you want to cache.
Parameters:
- ttl: TTL for the cached data (in seconds).
- max_size: Maximum number of cache entries.
- applying_params: List of parameter names to use as the cache key. If
None
, all parameters are used. If[]
, only the function name is used.
Member Functions:
- force_expire(key): Forces expiration of the cache entry for the specified key.
- is_exist(key): Checks if a specific key exists in the cache.
- get_item(key): Returns the cache item for the specified key.
- Note: The key can include partial elements of the cache key.
Usage:
- Add the
@TtlCache(ttl=seconds)
decorator to the function you want to cache. - Cache keys are generated in the format
"{class_name.}method_name(param1=value1, param2=value2, ...)"
. - To call the member functions of
TtlCache
, create an instance ofTtlCache
and use that instance as the decorator.
Example:
some_cache = TtlCache(ttl=5)
@some_cache
def some_function(x):
return x * 2
@TtlCache(ttl=5, max_size=10, applying_params=['key'])
def another_function(key, value):
return f'{key} = {value}'
# Usage
result = some_function(1)
some_cache.force_expire('some_function(x=1)')
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
Built Distribution
File details
Details for the file parametric-ttl-cache-0.1.0.tar.gz
.
File metadata
- Download URL: parametric-ttl-cache-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3e27228f6ddff473c65e6be8abfc616469d0d5fd9a901e814f87ad0574494b6 |
|
MD5 | 75ff4015bb550577614ab3bd26bd2c1e |
|
BLAKE2b-256 | 94e47c8feb80d0310a0c033f17cf3b690fec658314cdd99df3c3e98bec60b635 |
File details
Details for the file parametric_ttl_cache-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: parametric_ttl_cache-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad62abeed5aa0e0a2add5e6256f11dec459e1471ed0597d861a3fd9fb12ea38f |
|
MD5 | b2dc721b183333a65143f5c0c0f8ba80 |
|
BLAKE2b-256 | ac0d4ae9b2625a2c922b006966e0b6be6b5770d9fe02a435e5a14fd939098459 |