I have a working memoize decorator that uses the backend of a Django server to store the result of a function for a certain amount of time. I specifically apply this to a class method.
My decorator looks like this:
def memoize(prefix='mysite', timeout=300, keygenfunc=None):
def funcwrap(meth):
def keymaker(*args, **kwargs):
key = prefix + '___' + meth.func_name + '___' + keygenfunc(args, kwargs)
return key
def invalidate(*args, **kwargs):
key = keymaker(*args, **kwargs)
cache.set(key, None, 1)
def newfunc(*args, **kwargs):
key = keymaker(*args, **kwargs)
rv = cache.get(key)
if rv is None:
rv = meth(*args, **kwargs)
cache.set(key, rv, timeout)
return rv
newfunc.invalidate = invalidate
return newfunc
return funcwrap
I use this in a class method, so something like:
class StorageUnit(models.Model):
@memoize(timeout=60*180, keygenfunc=lambda x,y: str(x[0].id))
def someBigCalculation(self):
...
return result
The actual memoization process works great! That is, a challenge
myStorageUnitInstance.someBigCalculation()
uses cache correctly. Nice cool!
My problem is that I am trying to manually invalidate a record for a specific instance where I want to be able to run
myStorageUnitInstance.someBigCalculation.invalidate()
However, this does not work, because the "I" does not pass and, therefore, the key does not work. I get an IndexError: tuple index out of range error pointing to my lambda function, as shown earlier.
Of course, I can successfully call:
myStorageUnitInstance.someBigCalculation.invalidate(myStorageUnitInstance)
. "" , . Python , , "self"?