用django的参数缓存函数结果。
django-cache-fn的Python项目详细描述
缓存fn
使用django cache缓存具有超时的function(*arg, **kw)
。
快速启动
- 安装::
pip install django-cache-fn
- 开始在代码中使用它
from cache_fn.decorators import cache_fn
@cache_fn(prefix='myprefix', timeout=3600)
def foo(k1, k2):
return "%s %s"%(k1, k2)
用法
def cache_fn(timeout=1, prefix=None, cache_ttl=MEMCACHE_MAX_EXPIRATION):
"""
Retrieve data from cache if cacheable and no-stale,
otherise refresh synchronously and cache it.
* timeout: The stale timeout which would be handled in the decorator.
* prefix: The prefix of cache key.
* cache_ttl: The TTL(time to live) of key in memcache.
NOTE: For HttpResponse, we just cache the response whose status code is 200.
"""