redis-tagged-cache


Nameredis-tagged-cache JSON
Version 0.10.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2025-02-07 08:38:21
maintainerNone
docs_urlNone
authorFabien MARTY
requires_python<4.0,>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <!-- *** GENERATED FILE - DO NOT EDIT *** -->
<!-- This file was generated by jinja-tree (https://github.com/fabien-marty/jinja-tree) from the template file: README.md.template -->
    
# 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:

```python
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

```python
from rtc 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()

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

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

```

## Full API

You will find the full API in [this reference documentation](https://fabien-marty.github.io/redis-tagged-cache/reference/api/).

## 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](https://redis.io/docs/latest/develop/reference/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 poetry env will be automatically created)
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "redis-tagged-cache",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": "Fabien MARTY",
    "author_email": "fabien.marty@botify.com",
    "download_url": "https://files.pythonhosted.org/packages/7d/03/cbf6d88c219cda29e25ec844fa8d46a5b79559a3da0b99a5a612e7b0f6bb/redis_tagged_cache-0.10.0.tar.gz",
    "platform": null,
    "description": "<!-- *** GENERATED FILE - DO NOT EDIT *** -->\n<!-- This file was generated by jinja-tree (https://github.com/fabien-marty/jinja-tree) from the template file: README.md.template -->\n    \n# redis-tagged-cache\n\n## What is it?\n\n`redis-tagged-cache` is a Python 3.7+ cache library backed with Redis **with O(1) tags-based invalidation system**.\n\n### Low level example\n\nInstallation: `pip install redis-tagged-cache`\n\nUsage:\n\n```python\nfrom rtc import CacheMiss, RedisTaggedCache\n\ncache = RedisTaggedCache(\n    namespace=\"foo\",\n    host=\"localhost\",\n    port=6379,\n)\n\ninvalidation_tags = [\"tag1\", \"tag2\"]  # tags are only strings of your choice\n\n# Let's store something in the cache under the key \"key1\"\n# (with a 60s lifetime)\ncache.set(\"key1\", \"my value\", tags=invalidation_tags, lifetime=60)\n\n# it will output \"my value\" (cache hit!)\nprint(cache.get(\"key1\", tags=invalidation_tags))\n\n# Let's invalidate a tag (O(1) operation)\ncache.invalidate(\"tag2\")\n\n# As the \"key1\" entry is tagged with \"tag1\" and \"tag2\"...\n# ...the entry is invalidated (because we just invalidated \"tag2\")\n\n# It will print \"cache miss\"\ntry:\n    cache.get(\"key1\", tags=invalidation_tags)\nexcept CacheMiss:\n    print(\"cache miss\")\n\n```\n\n### High level example\n\n```python\nfrom rtc import RedisTaggedCache\n\ncache = RedisTaggedCache(\n    namespace=\"foo\",\n    host=\"localhost\",\n    port=6379,\n)\n\n\nclass A:\n    @cache.method_decorator(lifetime=60, tags=[\"tag1\", \"tag2\"])\n    def slow_method(self, arg1: str, arg2: str = \"foo\"):\n        print(\"called\")\n        return arg1 + arg2\n\n\nif __name__ == \"__main__\":\n    a = A()\n\n    # It will output \"called\" and \"foobar\" (cache miss)\n    print(a.slow_method(\"foo\", arg2=\"bar\"))\n\n    # It will output \"foobar\" (cache hit)\n    print(a.slow_method(\"foo\", arg2=\"bar\"))\n\n    # It will output \"called\" and \"foo2bar\" (cache miss)\n    print(a.slow_method(\"foo2\", arg2=\"bar\"))\n\n# Note: for plain functions, you can use @cache.function_decorator\n#       that works the same way\n\n```\n\n## Full API\n\nYou will find the full API in [this reference documentation](https://fabien-marty.github.io/redis-tagged-cache/reference/api/).\n\n## Pros & Cons\n\n### Pros\n\nAll 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.\n\nNote: complexity is O(n) regarding the number of tags.\n\n### Cons\n\nThe 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.\n\n**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](https://redis.io/docs/latest/develop/reference/eviction/)**\n\n## Dev\n\nAs we support Python 3.7+ for the runtime, the dev environment requires Python 3.9+.\n\n- `make lint`: for linting the code\n- `make test`: for executing test\n\n(the poetry env will be automatically created)",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.10.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d6627f235b79418ffdf60118942a592d410f988a368855ae1dfdb1dc76e6463e",
                "md5": "e602c79afaaf66c3a7d88a89dd40b221",
                "sha256": "5ed44dde505f99afefcf7e11245226038eb03619cae46063c2c8c5a604de2879"
            },
            "downloads": -1,
            "filename": "redis_tagged_cache-0.10.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e602c79afaaf66c3a7d88a89dd40b221",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.7",
            "size": 16250,
            "upload_time": "2025-02-07T08:38:20",
            "upload_time_iso_8601": "2025-02-07T08:38:20.655291Z",
            "url": "https://files.pythonhosted.org/packages/d6/62/7f235b79418ffdf60118942a592d410f988a368855ae1dfdb1dc76e6463e/redis_tagged_cache-0.10.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7d03cbf6d88c219cda29e25ec844fa8d46a5b79559a3da0b99a5a612e7b0f6bb",
                "md5": "2390e4bcec0cd7e97b0006b5b4e87fcb",
                "sha256": "605cfca29265194d7944f80eec421d1fa5d7c8b6e480a3da0a58b99f5df70463"
            },
            "downloads": -1,
            "filename": "redis_tagged_cache-0.10.0.tar.gz",
            "has_sig": false,
            "md5_digest": "2390e4bcec0cd7e97b0006b5b4e87fcb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.7",
            "size": 13807,
            "upload_time": "2025-02-07T08:38:21",
            "upload_time_iso_8601": "2025-02-07T08:38:21.663613Z",
            "url": "https://files.pythonhosted.org/packages/7d/03/cbf6d88c219cda29e25ec844fa8d46a5b79559a3da0b99a5a612e7b0f6bb/redis_tagged_cache-0.10.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-07 08:38:21",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "redis-tagged-cache"
}
        
Elapsed time: 1.58776s