def some_bigger_function(): db_set(1, 'something') value = simple_function1() db_set(2, 'something else') #### now we know we used 2 cached functions so.... invalidate('key1') invalidate('key2') #### now we know we are safe, but for a price return simple_function2()
def inner_workings(): db_set(1, 'something totally new')
@cached("key1") def simple_function1(): return db_get(id=1) @cached("key2") def simple_function2(): return db_get(id=2) # SUPPOSE THIS IS IN ANOTHER MODULE @cached("key") def some_bigger_function(): return {"1": simple_function1(), "2": simple_function2(), "3": db_get(id=3)} if __name__ == "__main__": simple_function1() # somewhere else db_set(1, "foobar") # and again db_set(3, "bazbar") invalidate("key") # ooops, we forgot something data = some_bigger_function() assert data["1"] == db_get(id=1), "this fails because we didn't manage to invalidate all the keys"