python简单模拟的本地轻量级缓存python
思路:缓存
# coding: utf-8 import weakref, collections class LocalCache(): notFound = object() # list dict等不支持弱引用,但其子类支持,故这里包装了下 class Dict(dict): def __del__(self): pass def __init__(self, maxlen=10): self.weak = weakref.WeakValueDictionary() self.strong = collections.deque(maxlen=maxlen) @staticmethod def nowTime(): return int(time.time()) def get(self, key): value = self.weak.get(key, self.notFound) if value is not self.notFound: expire = value[r'expire'] if self.nowTime() > expire: return self.notFound else: return value else: return self.notFound def set(self, key, value): # strongRef做为强引用避免被回收 self.weak[key] = strongRef = LocalCache.Dict(value) # 放入定大队列,弹出元素立刻被回收 self.strong.append(strongRef) # 装饰器 from functools import wraps def funcCache(expire=0): caches = LocalCache() def _wrappend(func): @wraps(func) def __wrapped(*args, **kwargs): key = str(func) + str(args) + str(kwargs) result = caches.get(key) if result is LocalCache.notFound: result = func(*args, **kwargs) caches.set(key, {r'result': result, r'expire': expire + caches.nowTime()}) result = caches.get(key) return result return __wrapped return _wrappend # 测试函数 import time @funcCache(expire=300) def test_cache(v): # 模拟任务处理时常3秒 time.sleep(3) print('work 3s') return v print(test_cache(1)) print(test_cache(2)) print(test_cache(1)) print(test_cache(2))
输出:app
work 3s
{'expire': 1524363279, 'result': 1}
work 3s
{'expire': 1524363282, 'result': 2}
{'expire': 1524363279, 'result': 1}
{'expire': 1524363282, 'result': 2}函数
可见第一次走具体处理函数,后面走的缓存测试