本文整理汇总了Python中gluon.current.cache方法的典型用法代码示例。如果您正苦于以下问题:Python current.cache方法的具体用法?Python current.cache怎么用?Python current.cache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gluon.current
的用法示例。
在下文中一共展示了current.cache方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def __call__(self, key, f,
time_expire=DEFAULT_TIME_EXPIRE):
"""
Tries to retrieve the value corresponding to `key` from the cache if the
object exists and if it did not expire, else it calls the function `f`
and stores the output in the cache corresponding to `key`. It always
returns the function that is returned.
Args:
key(str): the key of the object to be stored or retrieved
f(function): the function whose output is to be cached.
If `f` is `None` the cache is cleared.
time_expire(int): expiration of the cache in seconds.
It's used to compare the current time with the time
when the requested object was last saved in cache. It does not
affect future requests. Setting `time_expire` to 0 or negative
value forces the cache to refresh.
"""
raise NotImplementedError
示例2: initialize
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def initialize(self):
if self.initialized:
return
else:
self.initialized = True
folder = self.folder
request = self.request
# Lets test if the cache folder exists, if not
# we are going to create it
folder = os.path.join(folder or request.folder, 'cache')
if not os.path.exists(folder):
os.mkdir(folder)
self.storage = CacheOnDisk.PersistentStorage(folder)
示例3: __init__
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def __init__(self, request):
"""
Args:
request: the global request object
"""
# GAE will have a special caching
if have_settings and settings.global_settings.web2py_runtime_gae:
from gluon.contrib.gae_memcache import MemcacheClient
self.ram = self.disk = MemcacheClient(request)
else:
# Otherwise use ram (and try also disk)
self.ram = CacheInRam(request)
try:
self.disk = CacheOnDisk(request)
except IOError:
logger.warning('no cache.disk (IOError)')
except AttributeError:
# normally not expected anymore, as GAE has already
# been accounted for
logger.warning('no cache.disk (AttributeError)')
示例4: lazy_cache
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def lazy_cache(key=None, time_expire=None, cache_model='ram'):
"""
Can be used to cache any function including ones in modules,
as long as the cached function is only called within a web2py request
If a key is not provided, one is generated from the function name
`time_expire` defaults to None (no cache expiration)
If cache_model is "ram" then the model is current.cache.ram, etc.
"""
def decorator(f, key=key, time_expire=time_expire, cache_model=cache_model):
key = key or repr(f)
def g(*c, **d):
from gluon import current
return current.cache(key, time_expire, cache_model)(f)(*c, **d)
g.__name__ = f.__name__
return g
return decorator
示例5: initialize
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def initialize(self):
if self.initialized:
return
else:
self.initialized = True
folder = self.folder
request = self.request
# Lets test if the cache folder exists, if not
# we are going to create it
folder = os.path.join(folder or request.folder, 'cache')
if not os.path.exists(folder):
os.mkdir(folder)
self.storage = CacheOnDisk.PersistentStorage(folder)
if not CacheAbstract.cache_stats_name in self.storage:
self.storage[CacheAbstract.cache_stats_name] = {'hit_total': 0, 'misses': 0}
示例6: __init__
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def __init__(self, request):
"""
Args:
request: the global request object
"""
# GAE will have a special caching
if have_settings and settings.global_settings.web2py_runtime_gae:
from gluon.contrib.gae_memcache import MemcacheClient
self.ram = self.disk = MemcacheClient(request)
else:
# Otherwise use ram (and try also disk)
self.ram = CacheInRam(request)
try:
self.disk = CacheOnDisk(request)
except IOError:
logger.warning('no cache.disk (IOError)')
except AttributeError:
# normally not expected anymore, as GAE has already
# been accounted for
logger.warning('no cache.disk (AttributeError)')
示例7: clear
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def clear(self, regex=None):
"""
Clears the cache of all keys that match the provided regular expression.
If no regular expression is provided, it clears all entries in cache.
Args:
regex: if provided, only keys matching the regex will be cleared,
otherwise all keys are cleared.
"""
raise NotImplementedError
示例8: _clear
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def _clear(self, storage, regex):
"""
Auxiliary function called by `clear` to search and clear cache entries
"""
r = re.compile(regex)
for key in list(storage.keys()):
if r.match(str(key)):
del storage[key]
return
示例9: with_prefix
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def with_prefix(cache_model, prefix):
"""
allow replacing cache.ram with cache.with_prefix(cache.ram,'prefix')
it will add prefix to all the cache keys used.
"""
return lambda key, f, time_expire=DEFAULT_TIME_EXPIRE, prefix=prefix: cache_model(prefix + key, f, time_expire)
示例10: _clear
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def _clear(self, storage, regex):
"""
Auxiliary function called by `clear` to search and clear cache entries
"""
r = re.compile(regex)
for key in storage.keys():
if r.match(str(key)):
del storage[key]
return
示例11: with_prefix
# 需要导入模块: from gluon import current [as 别名]
# 或者: from gluon.current import cache [as 别名]
def with_prefix(cache_model, prefix):
"""
allow replacing cache.ram with cache.with_prefix(cache.ram,'prefix')
it will add prefix to all the cache keys used.
"""
return lambda key, f, time_expire=DEFAULT_TIME_EXPIRE, prefix=prefix:\
cache_model(prefix + key, f, time_expire)