redis-cached


Nameredis-cached JSON
Version 0.4.4 PyPI version JSON
download
home_pagehttps://github.com/dmitrybabanovforreal/redis-cached
SummaryPython cache decorator and other itils with support for Redis or KeyDB
upload_time2024-11-21 08:08:33
maintainerNone
docs_urlNone
authorDmitry Babanov
requires_python<3.13,>=3.11
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # redis-cached
Python cache decorator that uses Redis or KeyDB as storage. This is very handy for replicated apps (e.g. Kubernetes), AWS Lambda functions, and other stateless apps.

Features:
* Function result and kwarg values are [pickled](https://docs.python.org/3/library/pickle.html), so you can work with complex structures like [pydantic](https://docs.pydantic.dev/latest/)'s `BaseModel`
* Prevents multiple simultaneous cache updates when a function is called concurrently and there is no cached value.
* Cache invalidation is available

Limitations:
* Only async functions are supported.
* Only keyword arguments are supported. It will raise an error if you pass non-kwargs while calling your function.

# Installation

```shell
pip install redis_cached
```

# Usage

Basic usage:

```python
import asyncio
from redis_cached import Cache

cache = Cache()

@cache.cached(5)
async def get_user_data(user_id: int):
    # expensive API call here
    return {'user_id': user_id, 'name': 'John Doe'}

async def main():
    user_data = await get_user_data(user_id=1)  # result is cached for 5 seconds
    
    # To invalidate the cache for the user with ID 1, pass the same kwargs:
    await cache.invalidate_cache('get_user_data', user_id=1)

asyncio.run(main())
```


Optionally, add salt to `cache_key_salt` to avoid clashing with the same-named functions in other modules or other apps that use the same Redis database.

You can also use your custom-configured async Redis instance with the cache.

```python
import asyncio
from redis.asyncio.client import Redis
from redis_cached import Cache

custom_redis = Redis(
    host='localhost',
    port=6379,
    db=0
)
cache = Cache(
    cache_key_salt='qhDwh9Y',
    redis_=custom_redis
)

@cache.cached(5)
async def get_user_data(user_id: int):
    # expensive API call here
    return {'user_id': user_id, 'name': 'John Doe'}

async def main():
    user_data = await get_user_data(user_id=1)  # cached
    await cache.invalidate_cache('get_user_data', user_id=1)  # invalidated

asyncio.run(main())
```

# Contributing

Contributions are welcome. Please refer to the [maintenance readme](./maintenance/README.md) for more details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dmitrybabanovforreal/redis-cached",
    "name": "redis-cached",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.13,>=3.11",
    "maintainer_email": null,
    "keywords": null,
    "author": "Dmitry Babanov",
    "author_email": "85852989+dmitrybabanovforreal@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/52/2703caf72d8ef0cde534f2c40834f6c90bc68671e9e1936c742c28fd60ed/redis_cached-0.4.4.tar.gz",
    "platform": null,
    "description": "# redis-cached\nPython cache decorator that uses Redis or KeyDB as storage. This is very handy for replicated apps (e.g. Kubernetes), AWS Lambda functions, and other stateless apps.\n\nFeatures:\n* Function result and kwarg values are [pickled](https://docs.python.org/3/library/pickle.html), so you can work with complex structures like [pydantic](https://docs.pydantic.dev/latest/)'s `BaseModel`\n* Prevents multiple simultaneous cache updates when a function is called concurrently and there is no cached value.\n* Cache invalidation is available\n\nLimitations:\n* Only async functions are supported.\n* Only keyword arguments are supported. It will raise an error if you pass non-kwargs while calling your function.\n\n# Installation\n\n```shell\npip install redis_cached\n```\n\n# Usage\n\nBasic usage:\n\n```python\nimport asyncio\nfrom redis_cached import Cache\n\ncache = Cache()\n\n@cache.cached(5)\nasync def get_user_data(user_id: int):\n    # expensive API call here\n    return {'user_id': user_id, 'name': 'John Doe'}\n\nasync def main():\n    user_data = await get_user_data(user_id=1)  # result is cached for 5 seconds\n    \n    # To invalidate the cache for the user with ID 1, pass the same kwargs:\n    await cache.invalidate_cache('get_user_data', user_id=1)\n\nasyncio.run(main())\n```\n\n\nOptionally, add salt to `cache_key_salt` to avoid clashing with the same-named functions in other modules or other apps that use the same Redis database.\n\nYou can also use your custom-configured async Redis instance with the cache.\n\n```python\nimport asyncio\nfrom redis.asyncio.client import Redis\nfrom redis_cached import Cache\n\ncustom_redis = Redis(\n    host='localhost',\n    port=6379,\n    db=0\n)\ncache = Cache(\n    cache_key_salt='qhDwh9Y',\n    redis_=custom_redis\n)\n\n@cache.cached(5)\nasync def get_user_data(user_id: int):\n    # expensive API call here\n    return {'user_id': user_id, 'name': 'John Doe'}\n\nasync def main():\n    user_data = await get_user_data(user_id=1)  # cached\n    await cache.invalidate_cache('get_user_data', user_id=1)  # invalidated\n\nasyncio.run(main())\n```\n\n# Contributing\n\nContributions are welcome. Please refer to the [maintenance readme](./maintenance/README.md) for more details.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python cache decorator and other itils with support for Redis or KeyDB",
    "version": "0.4.4",
    "project_urls": {
        "Homepage": "https://github.com/dmitrybabanovforreal/redis-cached"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "aa559abd2b8bc4df7595858a61233014122b0ec7634a20e1819ec5c834a349f8",
                "md5": "7f046309dd557987d88eeae415f4e059",
                "sha256": "727a4247dd15030c6e5fd0415ed0a730f1a205bf9c53260f6df13d76123bd0be"
            },
            "downloads": -1,
            "filename": "redis_cached-0.4.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7f046309dd557987d88eeae415f4e059",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.13,>=3.11",
            "size": 4936,
            "upload_time": "2024-11-21T08:08:31",
            "upload_time_iso_8601": "2024-11-21T08:08:31.739651Z",
            "url": "https://files.pythonhosted.org/packages/aa/55/9abd2b8bc4df7595858a61233014122b0ec7634a20e1819ec5c834a349f8/redis_cached-0.4.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3522703caf72d8ef0cde534f2c40834f6c90bc68671e9e1936c742c28fd60ed",
                "md5": "eefc4d861f5ca2c01c8d9c5ade5d6547",
                "sha256": "550ae4f1ab04465d4bbbb4ea68f69825713ab721d5b0dc5301c00e0526ec770e"
            },
            "downloads": -1,
            "filename": "redis_cached-0.4.4.tar.gz",
            "has_sig": false,
            "md5_digest": "eefc4d861f5ca2c01c8d9c5ade5d6547",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.13,>=3.11",
            "size": 4119,
            "upload_time": "2024-11-21T08:08:33",
            "upload_time_iso_8601": "2024-11-21T08:08:33.072518Z",
            "url": "https://files.pythonhosted.org/packages/c3/52/2703caf72d8ef0cde534f2c40834f6c90bc68671e9e1936c742c28fd60ed/redis_cached-0.4.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-21 08:08:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dmitrybabanovforreal",
    "github_project": "redis-cached",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "redis-cached"
}
        
Elapsed time: 0.57273s