您当前的位置: 首页 >  Python

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python编程:使用cachy缓存数据

彭世瑜 发布时间:2019-12-12 10:49:51 ,浏览量:2

cachy使用pickle对对象进行序列化 支持驱动 File,Redis,Memcached,Database

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

1、安装
pip install cachy
2、配置
from cachy import CacheManager

stores = {
    'default': 'file',

    'stores': {
        'file': {
            'driver': 'file',
            'path': 'cache'
        }
    }
}

cache = CacheManager(stores)
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)

4、使用装饰器

默认60 minutes

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

print(get_users())
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0481s