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

from redis_tagged_cache import RedisTaggedCache

cache = RedisTaggedCache(
    namespace="foo",
    redis_host="localhost",
    redis_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 10s lifetime)
cache.set("key1", b"value1", tags=invalidation_tags, lifetime=10)

print(cache.get("key1", tags=invalidation_tags))  # will output b"value1" (cache hit!)

# 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")

print(cache.get("key1", tags=invalidation_tags))  # will output None (cache miss!)

High level example

from redis_tagged_cache import RedisTaggedCache

cache = RedisTaggedCache(
    namespace="foo",
    redis_host="localhost",
    redis_port=6379,
)

class A:

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

if __name__ == "__main__":
    a = A()
    print(a.slow_method("foo", arg2="bar"))  # will output "called" and "foobar" (cache miss)
    print(a.slow_method("foo", arg2="bar"))  # will output "foobar" (cache hit)
    print(a.slow_method("foo2", arg2="bar"))  # will output "called" and "foo2bar" (cache miss)

# Note: for plain functions, you can use @cache.function_decorator that works the same way

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 and FIXME setting about LRU

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 poetry env 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.0.2.tar.gz (7.1 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.0.2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: redis_tagged_cache-0.0.2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for redis_tagged_cache-0.0.2.tar.gz
Algorithm Hash digest
SHA256 497c6157b30f9e762079ee7dbe37d0a65078af11c6038115e8f23c56cda14c14
MD5 f7f79065bef32b49c054af087f5cd15b
BLAKE2b-256 cd5bc06b7c184106f4ca2eae3abcec658818ee0f78af8e38e96b9c6b8b67175b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: redis_tagged_cache-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for redis_tagged_cache-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aad85716939383849cf03cccbccb9b60671606d9bc082c9a9aa710c70839af0e
MD5 7ec005bf36817551c75e229077362254
BLAKE2b-256 21c3c0978974a1ca93dd7a774518df70460a43fc94c538ebafa434e574568049

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