# redis-cache-tools
It provides simple decorators that can be added to any function to cache its return values.
Requirements:
-------------
redis 4.0.0
Installation:
-------------
pip install redis-cache-tools
Usage:
------
from redis_cache import cache_it_json
@cache_it_json(limit=1000, expire=60 * 60 * 24)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
`limit` is the maximum number of keys, `expire` is the expire time in seconds.
It is always recommended to specify a expire time, since by default redis-server will only remove keys with an expire time set in a event of full memory. But if you wish your keys to never expire, set `expire` to `None`.
**Note that function arguments and result must be pickleable, since cache_it uses the pickle module.**
It is also possible to use redis-cache-tools as a object-oriented cache:
>> from redis_cache import SimpleCache
>> c = SimpleCache(10) # cache that has a maximum limit of 10 keys
>> c.store("foo", "bar")
>> c.get("foo")
'bar'
>> "foo" in c # efficient membership test, time-complexity O(1)
True
>> len(c) # efficient cardinality calculation, time-complexity O(1)
1
>> c.keys() # returns all keys, time-complexity O(N) with N being the cache c cardinality
set(['foo'])
>> c.flush() # flushes the cache, time-complexity O(N) with N being the cache c cardinality
>> "foo" in c
False
>> len(c)
0
Check out more examples in the test_rediscache.py file.
Advanced:
---------
Advanced users can customize the decorators even more by passing a SimpleCache object. For example:
using env variables.
export REDIS_HOST=localhost
export REDIS_PORT=6379
export REDIS_PORT=3
my_cache = SimpleCache(limit=100, expire=60 * 60, hashkeys=True, host='localhost', port=6379, db=1, namespace='Fibonacci')
@cache_it(cache=my_cache)
def fib(n):
# ...
`hashkeys` parameter makes the SimpleCache to store keys in md5 hash. It is `True` by default in decorators, but `False` by default in a new SimpleCache object.
`host`, `port` and `db` are the same redis config params used in StrictRedis class of redis-py.
By default, the `namespace` is the name of the module from which the decorated function is called, but it can be overridden with the `namespace` parameter.
Raw data
{
"_id": null,
"home_page": "https://500apps.com",
"name": "redis-cache-tools",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "redis-cache-tools",
"author": "It provides simple decorators that can be added to any function to cache its return values.",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/f6/43/2477c23958a730dd296d7e4094472ab1a64f1168f4fb750724a551f65081/redis-cache-tools-1.1.1.tar.gz",
"platform": null,
"description": "# redis-cache-tools\nIt provides simple decorators that can be added to any function to cache its return values.\n\nRequirements:\n-------------\nredis 4.0.0\n\nInstallation:\n-------------\n\n pip install redis-cache-tools\n\nUsage:\n------\n\n from redis_cache import cache_it_json\n\n @cache_it_json(limit=1000, expire=60 * 60 * 24)\n def fib(n):\n if n == 0:\n return 0\n elif n == 1:\n return 1\n else:\n return fib(n-1) + fib(n-2)\n\n`limit` is the maximum number of keys, `expire` is the expire time in seconds. \nIt is always recommended to specify a expire time, since by default redis-server will only remove keys with an expire time set in a event of full memory. But if you wish your keys to never expire, set `expire` to `None`. \n**Note that function arguments and result must be pickleable, since cache_it uses the pickle module.**\n\nIt is also possible to use redis-cache-tools as a object-oriented cache:\n \n >> from redis_cache import SimpleCache\n >> c = SimpleCache(10) # cache that has a maximum limit of 10 keys\n >> c.store(\"foo\", \"bar\")\n >> c.get(\"foo\")\n 'bar'\n >> \"foo\" in c # efficient membership test, time-complexity O(1)\n True\n >> len(c) # efficient cardinality calculation, time-complexity O(1)\n 1\n >> c.keys() # returns all keys, time-complexity O(N) with N being the cache c cardinality\n set(['foo'])\n >> c.flush() # flushes the cache, time-complexity O(N) with N being the cache c cardinality\n >> \"foo\" in c\n False\n >> len(c)\n 0\n\nCheck out more examples in the test_rediscache.py file.\n\nAdvanced:\n---------\nAdvanced users can customize the decorators even more by passing a SimpleCache object. For example:\n\n using env variables.\n\n export REDIS_HOST=localhost\n export REDIS_PORT=6379\n export REDIS_PORT=3\n \n my_cache = SimpleCache(limit=100, expire=60 * 60, hashkeys=True, host='localhost', port=6379, db=1, namespace='Fibonacci')\n @cache_it(cache=my_cache)\n def fib(n):\n # ...\n\n`hashkeys` parameter makes the SimpleCache to store keys in md5 hash. It is `True` by default in decorators, but `False` by default in a new SimpleCache object. \n`host`, `port` and `db` are the same redis config params used in StrictRedis class of redis-py.\nBy default, the `namespace` is the name of the module from which the decorated function is called, but it can be overridden with the `namespace` parameter. ",
"bugtrack_url": null,
"license": "",
"summary": "redis-cache-tools is a pythonic interface for creating a cache over redis.",
"version": "1.1.1",
"split_keywords": [
"redis-cache-tools"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "186cf5fa9bb529b11a94363fede9f810",
"sha256": "80d1b5331509aa5672b9dc47bea8ce59e7ed50e5ca27daffb7037f0f051fa9ea"
},
"downloads": -1,
"filename": "redis_cache_tools-1.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "186cf5fa9bb529b11a94363fede9f810",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 6259,
"upload_time": "2022-12-09T09:59:51",
"upload_time_iso_8601": "2022-12-09T09:59:51.312779Z",
"url": "https://files.pythonhosted.org/packages/8b/50/c26902ed3a33fc4c37ad14c1dacec1d036d8862fde6aefd57bd71400dccc/redis_cache_tools-1.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "14660e8810e388c2ab5bcf81dbc87619",
"sha256": "782d63833380753a743438f11b96728469f007bc7bd565218aa305b7b2b2e99d"
},
"downloads": -1,
"filename": "redis-cache-tools-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "14660e8810e388c2ab5bcf81dbc87619",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 6191,
"upload_time": "2022-12-09T09:59:49",
"upload_time_iso_8601": "2022-12-09T09:59:49.724081Z",
"url": "https://files.pythonhosted.org/packages/f6/43/2477c23958a730dd296d7e4094472ab1a64f1168f4fb750724a551f65081/redis-cache-tools-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-09 09:59:49",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "redis-cache-tools"
}