Skip to content

Add a descriptor to clear foreign cache #2

@vaab

Description

@vaab

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions