diskcache 模块
创建cache对象
from diskcache import Cache
cache = Cache()
往缓存内存东西
- 像字典一样存储
cache['a'] = 'A'
- set方法
cache.set('a', 'A', expire=5, tag='data')
- add方法
cache.add('a', 'A')
从缓存中取东西
cache['a']
- get方法
a = cache.get('a', read=True, tag=True)
a
>>> ("A","data")
cache.touch() 更新过期时间
cache.touch('a',expire=1111)
cache的加,减函数
cache.incr('a')
>>> 1
cache.decr('b', default=-9)
>>> -10
cache.pop() 从缓存中弹出值
cache.clear() 清除所有值
cache.evict() 从缓存中统计tag为xx的个数
for num in range(100):
_ = cache.set(num, num, tag= 'odd' if num % 2 else 'even')
cache.evict('even')
>>> 50
cache.push() 把值压入cache 返回key值
cache.pull() 从cache中取值,同pop
Index
from diskcache import Index
index = Index([('a',1),('b',2),('c3',3)])
'b' in index
>>> True
index['c']
>>> 3
del index['a']
len(index)
>>> 2