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

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第三方库


热门话题
面向批处理的java非阻塞队列   java如何基于HttpComponent获取html内容   java为什么我的程序会显示此错误?   java spring控制器如何处理应用程序/octetstream请求?   尝试将数据插入数据库时出现java常规错误   无法在java多线程处理中维护生产者任务的顺序   java为什么JSON数据无法访问ViewPager?   java获取Maven中特定分支的SVN buildnumber   java客户端无法从服务器接收信息   java等轴测地图绘制,生成   java无法调试ProcessBuilder   java热点JVM阵列分配   在数组中存储“inputdialog”数据的java   saml Java Inflater引发DataFormatException“无效代码长度集”   从集合(爬虫、Jsoup、Java)写入文件