缓存装饰器,使应用缓存更轻松

yoton的Python项目详细描述


https://travis-ci.org/ivannotes/yoton.svg?branch=master

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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用户界面如何在JavaGUI中为按钮创建各种颜色?   使用MSCAPI的RSA密钥包装的java使用   java Spring数据Redis JedisConnectionException:意外的流结束   java饼图未显示预期输出   java hystrixCommand注释commandKey的用途是什么   windows java可以从cmd运行jar,但不能通过双击来运行   java在单击按钮时遍历XML节点   java试图使用日期(int,int,int)构造函数   带有Buildship 2子项目的java不作为项目依赖项链接   java jsonrpc4j:如何从服务器获取列表<SomeObject>?   用于Java应用程序的内存设置MaxDirectMemory和MaxHeapMemory   java如何从以下类型格式化日期   javayoutube。搜索列表搜索不返回任何内容   java My参数在方法中不起作用,因为泛型存在问题   java如何将Map<Key1Type,Val1Type>转换为Map<Key1Type,Val2Type>   JavaJUnit测试:测试用例的改进,测试数组列表的长度?   java如何在Android中解析带有属性值的xml数据?   使用Hibernate对枚举类型进行Java8bean验证