Python实现LRUcache
介绍
设计一个简单通用的cache,具体思路:
- 遵循lru算法,lru:近期最少使用算法
- 使用python里的OrderDict来存储kv
- cache 有大小限制和过期限制
- 外部通过decorator方式来使用
实现
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
|
import time from collections import OrderedDict
class Cache(object): def init(self, expiration_time=10, cache_size=100): self.cache = OrderedDict() self.expiration_time = expiration_time self.cache_size = cache_size
def call(self, func): def _decorate(key): if self.cache.has_key(key): tm_point = self.cache[key].get(‘time’) if time.time() - tm_point > self.expiration_time: add_new = True else: add_new = False else: add_new = True
if add_new: value = func(key) if len(self.cache) == self.cache_size: self.cache.popitem(last=False) else: value = self.cache.pop(key).get(‘value’) cur = time.time() self.cache[key] = {‘time’: cur, ‘value’: value}
return value
return _decorate
|
测试一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| from cache import Cache as LRUcache
expiration_time=5 cache_size=10
@LRUcache(expiration_time, cache_size)) def get_auth(key): print ‘get a new’ value = key return value
def main(): for i in ‘a’ * 5: print get_auth(i)
if name == ‘main‘: main()
|
输出
说明只是第一次去调用了get_auth
,后面都是从cache取的。