Python:diskcache实现基于文件的数据缓存

diskcache是一个基于Sqlite文件的数据缓存

文档

示例

python 复制代码
from diskcache import Cache

# 指定文件夹
cache = Cache('./cache')

# 存
cache.set('name', 'tom')

# 取
print(cache.get('name'))

可以看到,目录下生成了3个文件

bash 复制代码
$ tree cache
cache
├── cache.db
├── cache.db-shm
└── cache.db-wal

设置过期时间

python 复制代码
import time

from diskcache import Cache


# 指定文件夹
cache = Cache('./cache')

# 存,单位:秒s
cache.set('name', 'tom', expire=1)

# 取
time.sleep(2)
print(cache.get('name'))
# None

参考文章

相关推荐
Predestination王瀞潞19 小时前
Python oct() 函数
开发语言·python
B站_计算机毕业设计之家19 小时前
python汽车数据分析可视化系统 爬虫 懂车帝 毕业设计 Django框架 vue框架 大数据✅
爬虫·python·数据分析·django·汽车·推荐算法·懂车帝
fl17683119 小时前
基于python+tkinter实现的自动组卷评卷考试系统python源码+课程设计+项目说明
开发语言·python·课程设计
王琦031819 小时前
Python 综合大作业
python
Dxy123931021619 小时前
Python自动连接已保存WiFi
开发语言·python
依旧很淡定20 小时前
Selenium(Python)创建Chrome浏览器实例
chrome·python·selenium
中文Python20 小时前
小白中文Python-db_桌面小黄鸭宠物
数据库·python·pygame·宠物·中文python·小白学python
MoRanzhi12031 天前
12. Pandas 数据合并与拼接(concat 与 merge)
数据库·人工智能·python·数学建模·矩阵·数据分析·pandas
qianmo20211 天前
基于gitpage实现网页托管的方式方法
python
235161 天前
【MySQL】MVCC:从核心原理到幻读解决方案
java·数据库·后端·sql·mysql·缓存