diskcache缓存存储

1,158 阅读1分钟

diskcache 模块

创建cache对象

from diskcache import Cache
cache = Cache()

往缓存内存东西

  1. 像字典一样存储
cache['a'] = 'A'
  1. set方法
cache.set('a', 'A', expire=5, tag='data')
  1. add方法
cache.add('a', 'A')

从缓存中取东西

  1. cache['a']
  2. 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