Python编程:使用cachy缓存数据

网友投稿 639 2022-05-30

cachy使用pickle对对象进行序列化

支持驱动 File,Redis,memcached,Database

文档

https://cachy.readthedocs.io/en/latest/installation.html

1、安装

pip install cachy

1

2、配置

from cachy import CacheManager stores = { 'default': 'file', 'stores': { 'file': { 'driver': 'file', 'path': 'cache' } } } cache = CacheManager(stores)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

3、CURD

# 添加 cache.put('key', 'value', 10) # 获取 value = cache.get('key') # print(value) # 检查存在 print(cache.has('key')) # cache.increment('key', 1) 报错 # 获取并且删除 value = cache.pull('key') # 不存在则添加 cache.add('key', 'value', 10) # 永久 cache.forever('key', 'value') # 移除 cache.forget('key') # 获取或更新 remember_forever 永久 value = cache.remember('key', 10, 'value') print(value)

1

2

3

4

5

6

7

8

9

Python编程:使用cachy缓存数据

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

4、使用装饰器

默认60 minutes

@cache def get_users(): print("查询数据库") return "查询结果" print(get_users())

1

2

3

4

5

6

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口测试如何在post请求中传递文件
下一篇:【ElasticSearch】结合laravel做高亮搜索
相关文章

 发表评论

暂时没有评论,来抢沙发吧~