diskcache是一个基于Sqlite文件的数据缓存
文档
- https://grantjenks.com/docs/diskcache/
- https://github.com/grantjenks/python-diskcache
- https://pypi.org/project/diskcache/
示例
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