Skip to content
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

Memoizaiton for consequent method calls on the same object #15

Open
Envek opened this issue Aug 5, 2023 · 1 comment
Open

Memoizaiton for consequent method calls on the same object #15

Envek opened this issue Aug 5, 2023 · 1 comment

Comments

@Envek
Copy link

Envek commented Aug 5, 2023

As far as I can tell from source code there is no memoization of fetched cache value for later cached method calls on the same object. Is it on purpose?

To be honest, this behavior seems to be pretty surprising:

class Foo
  include Cacheable
  cacheable :bar

  def bar
    "foo"
  end
end

foo = Foo.new

foo.bar # cache read and deserialization
foo.bar # cache read and deserialization
foo.bar # cache read and deserialization

Context: recently I saw a Rails app in the wild that cached some ActiveRecord model methods that in turn returned another ActiveRecord objects. And some places got terribly slow due to dozens of repeated calls to retrieve and deserialize which is especially slow and compute-intensive for AR objects, see this blog post for details (and yes, I know that doing so is a bad idea per se). So I had to get rid of cacheable in favor of pure memoization or manual caching with following memoization.

@mgod
Copy link

mgod commented Aug 15, 2023

You should be able to add your own cache adapter based on the :memory adapter that uses memoization and falls back on some other cache like the Rails.cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants