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

Enable to set value to memoized method externally #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ryoqun
Copy link

@ryoqun ryoqun commented Jun 2, 2014

Thanks for creating and maintaining this gem. :) We're currently evaluating to use this gem in our production code.

This pull request changes API. Namely it adds a new instance method called set_cache. This method can be used to set a memoized method to value from external.

In our code base, we need to assign the same value to huge number of objects at once in the batch processing for further manipulation of these objects.

In such a case, we would like to reuse the same object computed once at the entry point before the hot loop instead of repeatedly calling the memoized method for each and every objects.

So we'd like to have an official API to do that.

@ryoqun
Copy link
Author

ryoqun commented Jun 2, 2014

If this is sounds good to add, bumping a version is +1. :)

@matthewrudy
Copy link
Owner

Hi @ryoqun, I mostly maintain this for legacy support.
So I'm not sure I want to add new features.

But I wonder why you don't take a different approach if this is fresh code.
For doing something similar in my current project I do this

class User
  def comments
    @comments ||= find_comments
  end

  def sideload_comments(comments)
    @comments = comments
  end

  private

  def find_comments
    # some assumedly slow task to load comments
  end
end

Then your multi-user comment loader is something like.

def sideload_comments(users)
  all_comments = find_comments_for_users(users)
  users.zip(all_comments) do |user, comments|
    user.sideload_comments(comments)
  end
end

@pboling pboling mentioned this pull request Mar 26, 2015
@sebjacobs sebjacobs force-pushed the master branch 2 times, most recently from 9d66c95 to 34f90dd Compare November 8, 2019 21:05
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

Successfully merging this pull request may close these issues.

2 participants