本文整理汇总了Python中werkzeug.contrib.cache.SimpleCache.add方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleCache.add方法的具体用法?Python SimpleCache.add怎么用?Python SimpleCache.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.contrib.cache.SimpleCache
的用法示例。
在下文中一共展示了SimpleCache.add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HybridCache
# 需要导入模块: from werkzeug.contrib.cache import SimpleCache [as 别名]
# 或者: from werkzeug.contrib.cache.SimpleCache import add [as 别名]
class HybridCache(object):
"""
A hybrid cache class that provide in-process cache that is backup up by out-of-process cache
When setting a key it will set it in both in-process and out-of-process
When getting a key it will try to retrieve first from in-process and if not, from out-of-process
"""
def __init__(self, remote_addresses):
self.in_proc_cache = SimpleCache(1000, 3600) #todo - pass these are arguments
self.remote_addresses = remote_addresses
self.out_proc_cache = MemcachedCache(remote_addresses)
def get(self, key):
"""
get an item from the hybrid cache
first in the in-process and then in the out-of-process
in case the key does not exist in both return None
if the value exist in out of process but not in-process then it is added
"""
val = self.in_proc_cache.get(key)
if val is None:
val = MemcachedCache(self.remote_addresses).get(key)
if val is not None:
self.in_proc_cache.add(key, val, None) #todo: for how long to cache?
return val
def add(self, key, value, timeout = 300):
"""
store a key-value in the hybrid cache - both in and out-off process
"""
#self.out_proc_cache.add(key, value, timeout = timeout)
MemcachedCache(self.remote_addresses).add(key, value, timeout)
self.in_proc_cache.add(key, value, timeout = timeout)