Skip to content

Notify successful fresh cache retrieval #46

@kitmonisit

Description

@kitmonisit

Hi, I'd like to propose an enhancement similar to verbose_cache.

I will call the new keyword argument notify_fresh. The wrapped function will return a tuple where the second value indicates if the value was fresh from cache.

This will enable logic outside the scope of the cached function to make decisions based on whether the data came from the cache.

For example, let's pretend we have a snobby server that hates being asked too many questions in rapid succession. If we get our answer fresh from the cache, then we can ask the next question right away:

@cachier()
def small_talk_to_snobby_server(query):
    response = "I'm fine, thank you!"
    return response

if __name__ == "__main__":
    response, is_fresh = small_talk_to_snobby_server(query="How are you?", notify_fresh=True)
    if not is_fresh:
        sleep(60)
    response = small_talk_to_snobby_server(query="What are you up to these days?")

Some self-critique

I find it rather clunky and API-breaking to make functions return a different data type than expected. I do not know any other way around this.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions