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 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", b"value1", tags=invalidation_tags, lifetime=60)
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",
host="localhost",
port=6379,
)
class A:
@cache.method_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()
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 (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 codemake test: for executing test
(the poetry env will be automatically created)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file redis_tagged_cache-0.3.0.tar.gz.
File metadata
- Download URL: redis_tagged_cache-0.3.0.tar.gz
- Upload date:
- Size: 8.4 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33b2ea21f0b7363686f897793181bf1033a7d726e51434eb289f28ea849e19ce
|
|
| MD5 |
acd55601120e18ff2d1a7efadbeb19dd
|
|
| BLAKE2b-256 |
ce8bfa93b6e605479473591f5493aa6a582de56bdb42f2a1f006bc026f4cca63
|
File details
Details for the file redis_tagged_cache-0.3.0-py3-none-any.whl.
File metadata
- Download URL: redis_tagged_cache-0.3.0-py3-none-any.whl
- Upload date:
- Size: 10.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1efaf50773e2a70406838c5b6c824051d385c18bcc25e92685d4c81ad32afc43
|
|
| MD5 |
f92ada302f0bba84b0dbdf39e357cc61
|
|
| BLAKE2b-256 |
05c0f0f4816f63de0cacbadfa948af7cd85a94e3838ae2daa5227a9c34865674
|