lazy_lazy_cache
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.00.0 (2012-07-16 13:15:59) dev
|
Version 2.00.0 (2012-07-16 14:10:19) dev
|
||||||
|
|||||||
+34
-12
@@ -35,7 +35,7 @@ except ImportError:
|
|||||||
|
|
||||||
logger = logging.getLogger("web2py.cache")
|
logger = logging.getLogger("web2py.cache")
|
||||||
|
|
||||||
__all__ = ['Cache']
|
__all__ = ['Cache', 'lazy_lazy_cache']
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_TIME_EXPIRE = 300
|
DEFAULT_TIME_EXPIRE = 300
|
||||||
@@ -379,6 +379,26 @@ class CacheOnDisk(CacheAbstract):
|
|||||||
storage.close()
|
storage.close()
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
class CacheAction(object):
|
||||||
|
def __init__(self,func,key,time_expire,cache,cache_model):
|
||||||
|
self.func = func
|
||||||
|
self.key = key
|
||||||
|
self.time_expire = time_expire
|
||||||
|
self.cache = cache
|
||||||
|
self.cache_model = cache_model
|
||||||
|
def __call__(self,*a,**b):
|
||||||
|
if not self.key:
|
||||||
|
key2 = self.func.__name__+':'+repr(a)+':'+repr(b)
|
||||||
|
else:
|
||||||
|
key2 = self.key.replace('%(name)s',self.func.__name__)\
|
||||||
|
.replace('%(args)s',str(a)).replace('%(vars)s',str(b))
|
||||||
|
cache_model = self.cache_model
|
||||||
|
if not cache_model or isinstance(cache_model,str):
|
||||||
|
cache_model = getattr(self.cache,cache_model or 'ram')
|
||||||
|
return cache_model(key2,
|
||||||
|
lambda a=a,b=b:self.func(*a,**b),
|
||||||
|
self.time_expire)
|
||||||
|
|
||||||
|
|
||||||
class Cache(object):
|
class Cache(object):
|
||||||
"""
|
"""
|
||||||
@@ -437,8 +457,8 @@ class Cache(object):
|
|||||||
|
|
||||||
:param key: the key of the object to be store or retrieved
|
:param key: the key of the object to be store or retrieved
|
||||||
:param time_expire: expiration of the cache in microseconds
|
:param time_expire: expiration of the cache in microseconds
|
||||||
:param cache_model: `cache.ram`, `cache.disk`, or other
|
:param cache_model: "ram", "disk", or other
|
||||||
(like `cache.memcache` if defined). It defaults to `cache.ram`.
|
(like "memcache" if defined). It defaults to "ram".
|
||||||
|
|
||||||
Notes
|
Notes
|
||||||
-----
|
-----
|
||||||
@@ -451,20 +471,22 @@ class Cache(object):
|
|||||||
If the function `f` is an action, we suggest using
|
If the function `f` is an action, we suggest using
|
||||||
`request.env.path_info` as key.
|
`request.env.path_info` as key.
|
||||||
"""
|
"""
|
||||||
if not cache_model:
|
|
||||||
cache_model = self.ram
|
|
||||||
|
|
||||||
def tmp(func):
|
def tmp(func,cache=self,cache_model=cache_model):
|
||||||
def action(*a,**b):
|
action = CacheAction(func,key,time_expire,self,cache_model)
|
||||||
key2 = key.replace('%(name)s',func.__name__).replace('%(args)s',str(a)).replace('%(vars)s',str(b))
|
|
||||||
return cache_model(key2, lambda a=a,b=b:func(*a,**b), time_expire)
|
|
||||||
action.__name___ = func.__name__
|
action.__name___ = func.__name__
|
||||||
action.__doc__ = func.__doc__
|
action.__doc__ = func.__doc__
|
||||||
return action
|
return action
|
||||||
|
|
||||||
return tmp
|
return tmp
|
||||||
|
|
||||||
|
def lazy_lazy_cache(key=None,time_expire=None,cache_model='ram'):
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user