Is there a Python caching library? [closed]

2019-01-21 05:25发布

I'm looking for a Python caching library but can't find anything so far. I need a simple dict-like interface where I can set keys and their expiration and get them back cached. Sort of something like:

cache.get(myfunction, duration=300)

which will give me the item from the cache if it exists or call the function and store it if it doesn't or has expired. Does anyone know something like this?

12条回答
够拽才男人
2楼-- · 2019-01-21 05:45

keyring is the best python caching library. You can use

keyring.set_password("service","jsonkey",json_res)

json_res= keyring.get_password("service","jsonkey")

json_res= keyring.core.delete_password("service","jsonkey")
查看更多
一夜七次
3楼-- · 2019-01-21 05:50

No one has mentioned shelve yet. https://docs.python.org/2/library/shelve.html

It isn't memcached, but looks much simpler and might fit your need.

查看更多
Viruses.
4楼-- · 2019-01-21 05:51

Look at gocept.cache on pypi, manage timeout.

查看更多
虎瘦雄心在
5楼-- · 2019-01-21 05:55

From Python 3.2 you can use the decorator @lru_cache from the functools library. It's a Last Recently Used cache, so there is no expiration time for the items in it, but as a fast hack it's very useful.

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)
查看更多
地球回转人心会变
6楼-- · 2019-01-21 05:58

Try redis, it is one of the cleanest and easiest solutions for applications to share data in a atomic way or if you have got some web server platform. Its very easy to setup, you will need a python redis client http://pypi.python.org/pypi/redis

查看更多
成全新的幸福
7楼-- · 2019-01-21 05:59

Look at bda.cache http://pypi.python.org/pypi/bda.cache - uses ZCA and is tested with zope and bfg.

查看更多
登录 后发表回答