Skip to main content

redis-tagged-cache

What is it?

redis-tagged-cache is a Python 3.7+ cache library backed with Redis with O(1) tags-based invalidation system.

Low level example

Installation: pip install redis-tagged-cache

Usage:

from rtc import CacheMiss, RedisTaggedCache

cache = RedisTaggedCache(
    namespace="foo",
    host="localhost",
    port=6379,
)

invalidation_tags = ["tag1", "tag2"]  # tags are only strings of your choice

# Let's store something in the cache under the key "key1"
# (with a 60s lifetime)
cache.set("key1", "my value", tags=invalidation_tags, lifetime=60)

# it will output "my value" (cache hit!)
print(cache.get("key1", tags=invalidation_tags))

# Let's invalidate a tag (O(1) operation)
cache.invalidate("tag2")

# As the "key1" entry is tagged with "tag1" and "tag2"...
# ...the entry is invalidated (because we just invalidated "tag2")

# It will print "cache miss"
try:
    cache.get("key1", tags=invalidation_tags)
except CacheMiss:
    print("cache miss")

High level example

from rtc import RedisTaggedCache

cache = RedisTaggedCache(
    namespace="foo",
    host="localhost",
    port=6379,
)


class A:
    @cache.decorator(lifetime=60, tags=["tag1", "tag2"])
    def slow_method(self, arg1: str, arg2: str = "foo"):
        print("called")
        return arg1 + arg2


if __name__ == "__main__":
    a = A()

    # It will output "called" and "foobar" (cache miss)
    print(a.slow_method("foo", arg2="bar"))

    # It will output "foobar" (cache hit)
    print(a.slow_method("foo", arg2="bar"))

    # It will output "called" and "foo2bar" (cache miss)
    print(a.slow_method("foo2", arg2="bar"))

Full API

You will find the full API in this reference documentation.

Pros & Cons

Pros

All methods have a O(1) complexity regarding the number of keys. The invalidation is synchronous and very fast event if you invalidate millions of keys.

Note: complexity is O(n) regarding the number of tags.

Cons

The invalidation system does not really remove keys from redis. Invalidated entries are inaccessible (from the API) but they are not removed when the invalidation occurred. They are going to expire by themselves.

Be sure to configure your redis instance as a cache with capped memory (see maxmemory configuration parameter) and maxmemory-policy allkeys-lru settings about keys automatic eviction

Dev

As we support Python 3.7+ for the runtime, the dev environment requires Python 3.9+.

  • make lint: for linting the code
  • make test: for executing test

(the venv will be automatically created)

Metadata

Release files for redis-tagged-cache 0.12.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for redis-tagged-cache 0.12.1
File Size Uploaded
redis_tagged_cache-0.12.1.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for redis-tagged-cache 0.12.1
File Interpreter ABI Platform
redis_tagged_cache-0.12.1-py3-none-any.whl Python 3 none any Details

Total release size: 40.0 kB

Release files / redis_tagged_cache-0.12.1.tar.gz

Download URL redis_tagged_cache-0.12.1.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8bb799c60bf7857766ffb722403e3b49c8e8429434fa4707cc7145b5710451a0
BLAKE2b-256 checksum
How to use checksums
d2f5fe119d3828b664a7a6a15f4d5b7c335b40502c20326a08bb2a00a775f52c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release files / redis_tagged_cache-0.12.1-py3-none-any.whl

Download URL redis_tagged_cache-0.12.1-py3-none-any.whl
Size 23.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5abd4d925d867ec852e8de1a49889669fb1ee38ca5341fe0cd35213a826a2c21
BLAKE2b-256 checksum
How to use checksums
137993202d522c62c327fa00f5e5692fb0b6d3eab74463ee5c7bdb9aaef61720
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release history Release notifications | RSS feed

This release

0.12.1 This release

2 release files

0.12.0

2 release files

0.11.2

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.1

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.11

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page