YoTon is an util for cache, it simplifies cache with a decorator.
$ pip install yotonredis_server_config = {
"default": {
"host": "localhost",
"port": 6379,
"db": 1
},
"server_a": {
"host": "localhost",
"port": 6378,
"db": 2,
}
}
yoton = YoTon(redis_server_config)@yoton.cache(key_pattern="dummy_cache_key", expire_seconds=60)
def dummy_func():
return "hello"
>> dummy_func() # call the function
"hello" set in the cacheThe cache key is using python's string format syntax, you can find it here
@youton.cache(key_pattern="dummy:{a}_{b}_{c}", expire_seconds=60)
def dummy_func_with_params(a, b, c=3):
return a + b + cComplex object in parameters
@youton.cache(key_pattern="dummy:{a.id}_{b.name}", expire_seconds=60)
def dummy_func_with_params(a, b):
return a + b@yoton.cache(key_pattern="dummy_cache_key", database="test", expire_seconds=60)
def dummy_func_database():
return "hello"@yoton.cache(key_pattern="dummy_cache_key", key_formatter=CustomizedFormatter(), expire_seconds=60)
def dummy_func_keyforamtter():
passclass DummyClass(object):
@yoton.cache(key_pattern="instance_method")
def instance_method(self):
return "hello"# call the function directly without touch cache
dummy_func_with_params.call(a=1, b=2, c=3)
# refresh cache data
dummy_func_with_params.refresh_cache(a=1, b=2, c=3)
# remove data in cache
dummy_func_with_params.delete_cache(a=1, b=2, c=3)