Skip to main content

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:

  1. Add the @TtlCache(ttl=seconds) decorator to the function you want to cache.
  2. Cache keys are generated in the format "{class_name.}method_name(param1=value1, param2=value2, ...)".
  3. To call the member functions of TtlCache, create an instance of TtlCache 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parametric-ttl-cache-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

parametric_ttl_cache-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

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

Hashes for parametric-ttl-cache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c3e27228f6ddff473c65e6be8abfc616469d0d5fd9a901e814f87ad0574494b6
MD5 75ff4015bb550577614ab3bd26bd2c1e
BLAKE2b-256 94e47c8feb80d0310a0c033f17cf3b690fec658314cdd99df3c3e98bec60b635

See more details on using hashes here.

File details

Details for the file parametric_ttl_cache-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for parametric_ttl_cache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad62abeed5aa0e0a2add5e6256f11dec459e1471ed0597d861a3fd9fb12ea38f
MD5 b2dc721b183333a65143f5c0c0f8ba80
BLAKE2b-256 ac0d4ae9b2625a2c922b006966e0b6be6b5770d9fe02a435e5a14fd939098459

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page