Skip to main content

Lightweight in-memory cache with TTL, LRU eviction, and tag-based invalidation

Project description

philiprehberger-cache-kit

Tests PyPI version License

Lightweight in-memory cache with TTL, LRU eviction, and tag-based invalidation.

Installation

pip install philiprehberger-cache-kit

Usage

Basic Cache

from philiprehberger_cache_kit import Cache

cache: Cache[str] = Cache(max_size=1000)

cache.set("key", "value")
print(cache.get("key"))  # "value"

TTL (Time-to-Live)

cache = Cache(default_ttl=60.0)  # 60 seconds default

cache.set("session", "abc123")           # uses default TTL
cache.set("temp", "data", ttl=5.0)       # expires in 5 seconds
cache.set("permanent", "data", ttl=None) # no expiry when default is set

LRU Eviction

cache = Cache(max_size=100)

# When full, least recently used entries are evicted first
# Expired entries are evicted before non-expired ones
for i in range(200):
    cache.set(f"key-{i}", f"value-{i}")

print(cache.size)  # 100

Tag-Based Invalidation

cache.set("user:1", user_data, tags={"users", "team-a"})
cache.set("user:2", user_data, tags={"users", "team-b"})
cache.set("post:1", post_data, tags={"posts", "team-a"})

# Invalidate all entries tagged "team-a"
removed = cache.invalidate_by_tag("team-a")
print(removed)  # 2

Other Operations

cache.has("key")        # check existence
"key" in cache          # same as has()
cache.delete("key")     # delete single entry
cache.keys()            # list all non-expired keys
len(cache)              # count of non-expired entries
cache.get_entry("key")  # get CacheEntry with tags, expires_at
cache.clear()           # remove everything

API

Method Description
Cache(max_size=1000, default_ttl=None) Create a new cache
.set(key, value, ttl=None, tags=None) Store a value
.get(key, default=None) Retrieve a value
.has(key) Check if key exists and is not expired
.delete(key) Remove a key
.invalidate_by_tag(tag) Remove all entries with a tag
.clear() Remove all entries
.keys() List non-expired keys
.get_entry(key) Get CacheEntry object (value, expires_at, tags)
.size Number of stored entries
len(cache) Number of non-expired entries
key in cache Check key existence

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

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

philiprehberger_cache_kit-0.2.5.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_cache_kit-0.2.5-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_cache_kit-0.2.5.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_cache_kit-0.2.5.tar.gz
Algorithm Hash digest
SHA256 183d5cf077a48a75cb795ebd876c569ee53c54eb92bb9bedcd0064e518d0edd4
MD5 6da53fbee466dae8390aba20557ee9ad
BLAKE2b-256 f100f3db5a571a55473f48ccbc7a9db4d1d839099d30ccf4f7160694d23e5973

See more details on using hashes here.

File details

Details for the file philiprehberger_cache_kit-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_cache_kit-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8150222bef7e4661ba7389c888c0d91e51a1b1c833d3bbbd9e4f6288a6082182
MD5 e809da5f5a30c9cd6eeb322279e4ed5f
BLAKE2b-256 b7704a4397fffbbf574877cb7135c9ea69f5ffa465ea087bd6b303b2a3068b89

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