kids.cache
kids.cache copied to clipboard
Add a descriptor to clear foreign cache
Often, after calculating a method, we know that we invalidated other cached values... A good example would be ORM, an instance of a record in the database needs cached elements and cached complex function dependent on data of the records, but would then gets invalidated when some write function is called.
This is a little bit like a reverse key: it's not anymore the responsibility of the cache to detect invalidation but foreign functions that could clear the cache. Notice that it's maybe a lesser way to tackle invalidated cache in most situation compared to find the correct key lambda, and I'd like to find a better scenario where it's clear that the key function can't handle it in an elegant way.
>>> class A(object):
... @cache
... def complex_fun_on_record_data(self):
... print 'Compute foo()'
... return 'hello'
...
... @classmethod
... @cache
... def bar(cls):
... print "Compute bar()"
... return "bye"
...
... @clear_cache(foo)
... @clear_cache(bar)
... def data_changed(self):
... pass