diskcache is a data cache based on Sqlite files
If you don’t want to use redis as a cache, you can use diskcache instead to reduce your dependence on redis.
document
- https://grantjenks.com/docs/diskcache/
- https://github.com/grantjenks/python-diskcache
- https://pypi.org/project/diskcache/
Install
pip install diskcache
Example
from diskcache import Cache
# 指定文件夹
cache = Cache('./cache')
# 存
cache.set('name', 'tom')
# 取
print(cache.get('name'))
As you can see, 3 files were generated in the directory
$ tree cache
cache
├── cache.db
├── cache.db-shm
└── cache.db-wal
Set expiration time
import time
from diskcache import Cache
# 指定文件夹
cache = Cache('./cache')
# 存,单位:秒s
cache.set('name', 'tom', expire=1)
# 取
time.sleep(2)
print(cache.get('name'))
# None