Skip to main content

No project description provided

Project description

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)

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

redis_tagged_cache-0.12.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

redis_tagged_cache-0.12.0-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file redis_tagged_cache-0.12.0.tar.gz.

File metadata

  • Download URL: redis_tagged_cache-0.12.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.5

File hashes

Hashes for redis_tagged_cache-0.12.0.tar.gz
Algorithm Hash digest
SHA256 3a2a20d588fe637856458664d06a70ac29562afeb9d18cd59c36193f786925f9
MD5 4904817339823afd884481fc6f8ad744
BLAKE2b-256 2826f4d4dc31abcc8960a655518b0097ee7f314a05b5c825b190506d48c75b15

See more details on using hashes here.

File details

Details for the file redis_tagged_cache-0.12.0-py3-none-any.whl.

File metadata

File hashes

Hashes for redis_tagged_cache-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 afb8966faa2c11131fc9f62493f29911eff9631b658ab26420f43d969e938620
MD5 ddaa2f0b8939fcb84314957d4b10d5f2
BLAKE2b-256 c4d0300b2ff0ffc50bc8159c38f24442fae1e671b1f716d5fcbcae924925ea22

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