Skip to main content

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

Project description

philiprehberger-cache-kit

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
cache.delete("key")     # delete single entry
cache.keys()            # list all non-expired keys
cache.clear()           # remove everything

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.1.1.tar.gz (3.4 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.1.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_cache_kit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 373351998bb279240433097f9f3f130d6a5a02d208e47ea793789373115e78ce
MD5 f67877748a568d3a333872c126098752
BLAKE2b-256 26d845b617a010a31d4205aff1e73ec5766716ddb20ffdd1efd708d3c354a627

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_cache_kit-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e3e7408d7af1be3e0277fae84d1e24870d27c1f6a04ceaaa5212237e26d81451
MD5 ce05ed5f6c23a72bfda3873c65f03ba6
BLAKE2b-256 85851bab741d84bbc44be67c26baa87e1d1bd77b06b6a00eb32dfe9dd1769167

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