缓存装饰器,使应用缓存更轻松
yoton的Python项目详细描述
yoton是缓存的一个实用工具,它使用decorator简化了缓存。
安装
$ pip install yoton
配置
redis_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)defdummy_func():return"hello">>dummy_func()# call the function"hello"setinthecache
按键模式
缓存键使用python的字符串格式语法,您可以在此处找到它
@youton.cache(key_pattern="dummy:{a}_{b}_{c}",expire_seconds=60)defdummy_func_with_params(a,b,c=3):returna+b+c
参数中的复杂对象
@youton.cache(key_pattern="dummy:{a.id}_{b.name}",expire_seconds=60)defdummy_func_with_params(a,b):returna+b
选择“数据库”
@yoton.cache(key_pattern="dummy_cache_key",database="test",expire_seconds=60)defdummy_func_database():return"hello"
自定义格式化程序
@yoton.cache(key_pattern="dummy_cache_key",key_formatter=CustomizedFormatter(),expire_seconds=60)defdummy_func_keyforamtter():pass
将缓存应用于实例方法
classDummyClass(object):@yoton.cache(key_pattern="instance_method")definstance_method(self):return"hello"
其他
# call the function directly without touch cachedummy_func_with_params.call(a=1,b=2,c=3)# refresh cache datadummy_func_with_params.refresh_cache(a=1,b=2,c=3)# remove data in cachedummy_func_with_params.delete_cache(a=1,b=2,c=3)