-
Notifications
You must be signed in to change notification settings - Fork 99
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
Memoize errors #20
base: master
Are you sure you want to change the base?
Memoize errors #20
Conversation
That's pretty cool. Not taking a stance on viability either way, but damn. That's cool. |
I would find this very useful |
For example, where the memoised function is expensive (eg network access, file IO or an expensive calculation) |
👍 Bump. I really think this makes sense, after thinking about it for almost a year... ⌚ If the goal of memoizable is to cache the result of a method call, this fixes a bug where it hasn't been doing that for exceptions. @njonsson Based on this comment I think this may require a fork and a new project with expanded scope. |
Thanks for your contribution. But... Do you really think that's expected behaviour? I guess it depends what you're trying to memoize. Eg. class User < ActiveRecord::Base
has_many :posts
def post_titles
posts.pluck(:title)
end
memoize :post_titles
end If this fails, then the database connection is dead. If the database connection is dead, then what do we do? Presumably we have a retry scheme. Or maybe that happens at a lower level So, I guess what I'm saying is "what's an example of an exception that we want to cache?" I can only think of exceptions that we don't want to cache. |
Now errors do not prevent memoization. If the method is called with the same arguments, the same Exception object is raised without an additional call to the method.
The API you established for memoized values applies to memoized errors, too. I’ve updated the MiniTest test to demonstrate that specifying the optional |
What about making it a configuration option?
and/or
|
9d66c95
to
34f90dd
Compare
I don’t know whether you’d consider this to be within the scope of Memoist, but I found a need for it.
As it stands, an error raised by a method prevents its memoization. This commit allows Memoist to cache not just successful results of a method but also errors. If a method is called with the same arguments, the same Exception object is raised without an additional call to the method.