public Method

MemCache.get_multi(*keys)

Retrieves multiple values from memcached in parallel, if possible.

The memcached protocol supports the ability to retrieve multiple keys in a single request. Pass in an array of keys to this method and it will:

  1. map the key to the appropriate memcached server
  2. send a single request to each server that has one or more key values

Returns a hash of values.

cache["a"] = 1
cache["b"] = 2
cache.get_multi "a", "b" # => { "a" => 1, "b" => 2 }

Source Code

# File active_support/vendor/memcache-client-1.5.0/memcache.rb, line 264
def get_multi(*keys)
  raise MemCacheError, 'No active servers' unless active?

  keys.flatten!
  key_count = keys.length
  cache_keys = {}
  server_keys = Hash.new { |h,k| h[k] = [] }

  # map keys to servers
  keys.each do |key|
    server, cache_key = request_setup key
    cache_keys[cache_key] = key
    server_keys[server] << cache_key
  end

  results = {}

  server_keys.each do |server, keys|
    keys = keys.join ' '
    values = if @multithread then
               threadsafe_cache_get_multi server, keys
             else
               cache_get_multi server, keys
             end
    values.each do |key, value|
      results[cache_keys[key]] = Marshal.load value
    end
  end

  return results
rescue TypeError, SocketError, SystemCallError, IOError => err
  handle_error server, err
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.