有一个Pythoncaching库吗?

我正在寻找一个Pythoncaching库,但到目前为止找不到任何东西。 我需要一个简单的dict的界面,在那里我可以设置键和过期时间,并让它们caching。 有点像这样的东西:

 cache.get(myfunction, duration=300) 

如果它存在或者调用该函数并将其存储(如果它不存在或已经过期),则将其从caching中提供给我。 有人知道这样的事吗?

看看烧杯:

  • 主页
  • caching文档
  • 有关在Django中使用Beaker的快速入门文章(但也适用于其他任何应用程序)

从Python 3.2中,您可以使用functools库中的装饰器@lru_cache 。 这是最近使用过的caching,所以其中的项目没有到期时间,但作为一个快速入侵,这是非常有用的。

 from functools import lru_cache @lru_cache(maxsize=256) def f(x): return x*x for x in range(20): print f(x) for x in range(20): print f(x) 

你也可以看看Memoize装饰器 。 你可能可以得到它做你想要的,没有太多的修改。

Joblib http://packages.python.org/joblib/支持Memoize模式中的cachingfunction。; 大多数情况下,这个想法是caching计算昂贵的function。

 >>> from joblib import Memory >>> mem = Memory(cachedir='/tmp/joblib') >>> import numpy as np >>> square = mem.cache(np.square) >>> >>> a = np.vander(np.arange(3)).astype(np.float) >>> b = square(a) ________________________________________________________________________________ [Memory] Calling square... square(array([[ 0., 0., 1.], [ 1., 1., 1.], [ 4., 2., 1.]])) ___________________________________________________________square - 0...s, 0.0min >>> c = square(a) 

你也可以做一些奇怪的事情,比如在函数上使用@ memory.cache装饰器。 文档在这里: http : //packages.python.org/joblib/memory.html

没有人提到搁置。 https://docs.python.org/2/library/shelve.html

它不是memcached,但看起来更简单,可能适合您的需要。

我认为Python memcached API是stream行的工具,但我没有使用它自己,不知道它是否支持你需要的function。

 import time class CachedItem(object): def __init__(self, key, value, duration=60): self.key = key self.value = value self.duration = duration self.timeStamp = time.time() def __repr__(self): return '<CachedItem {%s:%s} expires at: %s>' % (self.key, self.value, time.time() + self.duration) class CachedDict(dict): def get(self, key, fn, duration): if key not in self \ or self[key].timeStamp + self[key].duration < time.time(): print 'adding new value' o = fn(key) self[key] = CachedItem(key, o, duration) else: print 'loading from cache' return self[key].value if __name__ == '__main__': fn = lambda key: 'value of %s is None' % key ci = CachedItem('a', 12) print ci cd = CachedDict() print cd.get('a', fn, 5) time.sleep(2) print cd.get('a', fn, 6) print cd.get('b', fn, 6) time.sleep(2) print cd.get('a', fn, 7) print cd.get('b', fn, 7) 

试试redis,它是以primefaces的方式共享数据的应用程序的最干净和最简单的解决scheme之一,或者如果你有一些Web服务器平台。 它很容易设置,你将需要一个Python的Redis的客户端http://pypi.python.org/pypi/redis

您可以使用我的简单解决scheme来解决问题。 这真的很简单,没有什么奇特的:

 class MemCache(dict): def __init__(self, fn): dict.__init__(self) self.__fn = fn def __getitem__(self, item): if item not in self: dict.__setitem__(self, item, self.__fn(item)) return dict.__getitem__(self, item) mc = MemCache(lambda x: x*x) for x in xrange(10): print mc[x] for x in xrange(10): print mc[x] 

它确实缺乏到期function,但是您可以通过在MemCache c-tor中指定特定的规则来轻松扩展它。

希望代码足以说明问题,但如果不是这样的话,那么caching正在被作为其中一个parameter passing给一个翻译函数。 它被用来生成关于input的caching输出。

希望能帮助到你

看看gocept.cache

看看bda.cache http://pypi.python.org/pypi/bda.cache – 使用ZCA并用zope和bfg进行testing。

钥匙圈是最好的pythoncaching库。 您可以使用

 keyring.set_password("service","jsonkey",json_res) json_res= keyring.get_password("service","jsonkey") json_res= keyring.core.delete_password("service","jsonkey")