我有一个get(bid, mid, pid)
函数。它用lru_cache
装饰。例如,我想删除带有bid == 105
的所有缓存项
我在考虑一些闭包,它返回装饰过的函数。然后我为每个bid
条目获得一些单独的缓存,并使用这些闭包的dict获得非缓存函数,这些闭包的作用类似于路由器。但也许有一种更像Python的方法
upd:我想出了这样的办法,而且似乎奏效了
getters = {}
def facade(bid, mid, pid):
global getters # not very good, better to use class
if not bid in getters:
def create_getter(bid):
@functools.lru_cache(maxsize=None)
def get(mid, pid):
print ('cache miss')
return bid + mid + pid
return get
getters[bid] = create_getter(bid)
return getters[bid](mid, pid)
val = facade(bid, mid, pid) # ability to read like before
if need_to_drop:
getters[bid].cache_clear() # ability to flush entries with specified bid
也许包装
functools.lru_cache
和过滤参数输出:
相关问题 更多 >
编程相关推荐