Allow fetching multiple values from the cache at once #10234

merged 1 commit into from May 7, 2013

3 participants


This pull request adds a #fetch_multi to the base cache store class. The API works as follows:

cache.write("foo", "ding")
cache.fetch_multi("foo", "bar") { |value| value.reverse }
#=> ["ding", "rab"]"bar") #=> "rab"

The value of the addition is most obvious when dealing with objects which hold state and implement #cache_key:

content_tag :ul do
  cache.fetch_multi(*articles, :namespace => "something") do |article|
    # Assume that this is expensive...
    content_tag :li, link_to(article.title, article)
@dasch dasch referenced this pull request Apr 16, 2013
@dasch dasch Extract a base class from ActiveSupport::LogSubscriber
Adds a ActiveSupport::Subscriber base class that LogSubscriber inherits
from. By inheriting from Subscriber, other kinds of subscribers can take
advantage of the event attachment system.

Seems good. Could you squash your commits ?


@dmathieu done! Should I write a CHANGELOG entry as well, or is it not needed?


For this kind of change, which is more of something missing (as there is already read_multi) rather than something new, I wouldn't think so.

cc @carlosantoniodasilva @rafaelfranca


@carlosantoniodasilva what do you think of this PR? While #10268 may be a niche use case, I think this one is widely usable.


@dmathieu @carlosantoniodasilva sorry for being pushy, but could you give this another look?

Ruby on Rails member

Seems good. We will need the CHANGELOG entry

@dasch dasch Allow fetching multiple values from the cache at once
Add a simple API for fetching a list of entries from the cache, where
any missing entries are computed by a supplied block.
@rafaelfranca rafaelfranca merged commit 33283c9 into rails:master May 7, 2013
@dasch dasch deleted the dasch:dasch/fetch-multi branch May 10, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment