缓存概念
缓存是一种将定量数据加以保存以备迎合后续请求的处理方式,目的是为了加快数据的检索速度
下面看一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import datetime import random class MyCache: """""" def __init__(self): """Constructor""" self.cache = {} self.max_cache_size = 10 def __contains__(self, key): """ 根据该键是否存在于缓存当中返回True或者False """ return key in self.cache def update(self, key, value): """ 更新该缓存字典,您可选择性删除最早条目 """ if key not in self.cache and len(self.cache) >= self.max_cache_size: self.remove_oldest() self.cache[key] = {'date_accessed': datetime.datetime.now(), 'value': value} def remove_oldest(self): """ 删除具备最早访问日期的输入数据 """ oldest_entry = None for key in self.cache: if oldest_entry == None: oldest_entry = key elif self.cache[key]['date_accessed'] < self.cache[oldest_entry]['date_accessed']: oldest_entry = key self.cache.pop(oldest_entry) @property def size(self): """ 返回缓存容量大小 """ return len(self.cache) if __name__ == '__main__': keys = ['test', 'red', 'fox', 'fence', 'junk', \ 'other', 'alpha', 'bravo', 'cal', 'devo', 'ele'] s = 'abcdefghijklmnop' cache = MyCache() for i, key in enumerate(keys): if key in cache: continue else: value = ''.join([random.choice(s) for i in range(20)]) cache.update(key, value) print("#%s iterations, #%s cached entries" % (i+1, cache.size))
|