Hello, I have one method responsible for generate keys for cache based on the function parameters, like:
def get_cache_key(method, self, *args):
m = hashlib.md5()
m.update(repr(args))
key = m.hexdigest()
return key
And some functions uses ram.cache decorator
@ram.cache(get_cache_key)
def get_courses_by_year(self, year, campus):
...
This works.
I would like to pass key and a timeout value. Is it possible?
Thanks for your time
alert
(Alessandro Pisa)
2
Untested:
from time import time
class get_cache_key(object):
def __init__(self, seconds):
self.seconds = seconds
def __call__(self, method, instance, *args):
m = hashlib.md5()
m.update(repr([args + (time() // 300, )))
return m.hexdigest()
@ram.cache(get_cache_key(3600))
def get_courses_by_year(self, year, campus):
...
Thanks. With a few modifications in your code I got what I need.
from plone.registry.interfaces import IRegistry
from zope.component import getUtility
import hashlib
from time import time
class propg_cache_key(object):
def __call__(self, method, instance, *args):
m = hashlib.md5()
registry = getUtility(IRegistry)
cache_timeout = registry.get('ufscarpropg.cachetimeout')
m.update(repr(args + (time() // cache_timeout,)))
key = m.hexdigest()
return key