Previously eleith wrote:
> > My solution was to write my own decorator.
> could you share your decorator?
For what it's worth below is my variant of beaker_cache. It has
the advantage over the stock version that it works everywhere
instead of only for controller methods and will never try to
cache response headers. The only missing feature I can think of
is a simple way to add request headers to the cache key. That is
trivial to add, but not needed for my current projects so I haven't
added that.
Wichert.
def timed(expire=None, invalidate_on_startup=False, **b_kwargs):
"""Cache decorator utilizing Beaker. Caches action or other
function that returns a pickle-able object as a result.
Optional arguments:
``expire``
Time in seconds before cache expires, or the string "never".
Defaults to "never"
``invalidate_on_startup``
If True, the cache will be invalidated each time the application
starts or is restarted.
If cache_enabled is set to False in the .ini file, then cache is
disabled globally.
"""
if invalidate_on_startup:
starttime = time.time()
else:
starttime = None
def wrapper(func, *args, **kwargs):
"""Decorator wrapper"""
enabled = pylons.config.get("cache_enabled", "True")
if not asbool(enabled):
log.debug("Caching disabled, skipping cache lookup")
return func(*args, **kwargs)
key_dict=kwargs.copy()
key_dict.update(_make_dict_from_args(func, args))
cache_key = " ".join(["%s=%s" % (k, v) for k, v in
key_dict.iteritems()])
if hasattr(func, "im_class"):
namespace="%s.%s.%s" % (func.__module__, func.im_class.__name__,
func.__name__)
else:
namespace="%s.%s" % (func.__module__, func.__name__)
my_cache = pylons.cache.get_cache(namespace, **b_kwargs)
def create_func():
log.debug("Creating new cache copy with key: %s", cache_key)
return func(*args, **kwargs)
return my_cache.get_value(cache_key, createfunc=create_func,
expiretime=expire, starttime=starttime)
return decorator(wrapper)
--
Wichert Akkerman <[email protected]> It is simple to make things.
http://www.wiggy.net/ It is hard to make things simple.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---