-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query decorated function if result is from cache #51
Conversation
@shaypal5 I know this is not what you suggested in #46 (comment), but hear me out for a bit: I realized there was a need to encapsulate the |
API-wise, you're right ― there are existing precedents to modifying, and actually enhancing, the decorated function's API with additional attributes, such as However, the use case here is different. The practical implication is that this API is indeed not thread-safe: You might call I still think a simple listener object is the way to go, since it is bound to the call rather than the function. This is like future objects in various programming languages, that are implemented as per-call instances you get and query. So yeah, maybe something like this: call_info = cachier.Info()
foo(cachier_info=call_info)
if call_info.is_from cache:
# do something The more general name and structure of a call information object is intentional, since this is an easily expandable API, onto which we can graft additional functionalities for call-specific information (an obvious and useful example: get the time in which the value in the cache was produced). Let me know what you think! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment about the fundamental problems in this implementation - first among them is breaking thread-safety.
Ah, wonderful. And here I thought I was being thread-safe by localizing the query but as you point out, it's exactly the opposite. Let me take another swing at it! |
Regarding thread-safety, here is how I imagine a demonstration:
Is that how you envisioned it, @shaypal5? |
Just writing to say I didn't forget this. This is on my TO-DO list for very soon. :) |
Yes, that looks good! I'm sorry for the extremely belated response! |
Closed after being stale for a long time. |
Decorated function gains a new attribute function,
is_from_cache
, which returnsTrue
if the result comes from the cache; otherwiseFalse
.For example, if the decorated function is named
foo()
: