Class: ActiveSupport::Cache::MemCacheStore

Inherits:
Store show all
Defined in:
activesupport/lib/active_support/cache/mem_cache_store.rb

Overview

A cache store implementation which stores data in Memcached: www.danga.com/memcached/

This is currently the most popular cache store for production websites.

Special features:

  • Clustering and load balancing. One can specify multiple memcached servers, and MemCacheStore will load balance between all available servers. If a server goes down, then MemCacheStore will ignore it until it comes back up.

MemCacheStore implements the Strategy::LocalCache strategy which implements an in memory cache inside of a block.

Defined Under Namespace

Modules: LocalCacheWithRaw, Response

Constant Summary

ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/n

Instance Attribute Summary

Attributes inherited from Store

#options, #silence

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Store

#cleanup, #delete, #delete_matched, #exist?, #fetch, instrument, instrument=, #mute, #read, #silence!, #write

Constructor Details

- (MemCacheStore) initialize(*addresses)

Creates a new MemCacheStore object, with the given memcached server addresses. Each address is either a host name, or a host-with-port string in the form of "host_name:port". For example:

ActiveSupport::Cache::MemCacheStore.new("localhost", "server-downstairs.localnetwork:8229")

If no addresses are specified, then MemCacheStore will connect to localhost port 11211 (the default memcached port).

Instead of addresses one can pass in a MemCache-like object. For example:

require 'memcached' # gem install memcached; uses C bindings to libmemcached
ActiveSupport::Cache::MemCacheStore.new(Memcached::Rails.new("localhost:11211"))


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'activesupport/lib/active_support/cache/mem_cache_store.rb', line 56

def initialize(*addresses)
  addresses = addresses.flatten
  options = addresses.extract_options!
  super(options)

  if addresses.first.respond_to?(:get)
    @data = addresses.first
  else
    mem_cache_options = options.dup
    UNIVERSAL_OPTIONS.each{|name| mem_cache_options.delete(name)}
    @data = self.class.build_mem_cache(*(addresses + [mem_cache_options]))
  end

  extend Strategy::LocalCache
  extend LocalCacheWithRaw
end

Class Method Details

+ (Object) build_mem_cache(*addresses)



36
37
38
39
40
41
# File 'activesupport/lib/active_support/cache/mem_cache_store.rb', line 36

def self.build_mem_cache(*addresses)
  addresses = addresses.flatten
  options = addresses.extract_options!
  addresses = ["localhost:11211"] if addresses.empty?
  MemCache.new(addresses, options)
end

Instance Method Details

- (Object) clear(options = nil)

Clear the entire cache on all memcached servers. This method should be used with care when shared cache is being used.



118
119
120
# File 'activesupport/lib/active_support/cache/mem_cache_store.rb', line 118

def clear(options = nil)
  @data.flush_all
end

- (Object) decrement(name, amount = 1, options = nil)

Decrement a cached value. This method uses the memcached decr atomic operator and can only be used on values written with the :raw option. Calling it on a value not stored with :raw will initialize that value to zero.



106
107
108
109
110
111
112
113
114
# File 'activesupport/lib/active_support/cache/mem_cache_store.rb', line 106

def decrement(name, amount = 1, options = nil) # :nodoc:
  options = merged_options(options)
  response = instrument(:decrement, name, :amount => amount) do
    @data.decr(escape_key(namespaced_key(name, options)), amount)
  end
  response == Response::NOT_FOUND ? nil : response.to_i
rescue MemCache::MemCacheError
  nil
end

- (Object) increment(name, amount = 1, options = nil)

Increment a cached value. This method uses the memcached incr atomic operator and can only be used on values written with the :raw option. Calling it on a value not stored with :raw will initialize that value to zero.



92
93
94
95
96
97
98
99
100
# File 'activesupport/lib/active_support/cache/mem_cache_store.rb', line 92

def increment(name, amount = 1, options = nil) # :nodoc:
  options = merged_options(options)
  response = instrument(:increment, name, :amount => amount) do
    @data.incr(escape_key(namespaced_key(name, options)), amount)
  end
  response == Response::NOT_FOUND ? nil : response.to_i
rescue MemCache::MemCacheError
  nil
end

- (Object) read_multi(*names)

Reads multiple values from the cache using a single call to the servers for all keys. Options can be passed in the last argument.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'activesupport/lib/active_support/cache/mem_cache_store.rb', line 75

def read_multi(*names)
  options = names.extract_options!
  options = merged_options(options)
  keys_to_names = Hash[names.map{|name| [escape_key(namespaced_key(name, options)), name]}]
  raw_values = @data.get_multi(keys_to_names.keys, :raw => true)
  values = {}
  raw_values.each do |key, value|
    entry = deserialize_entry(value)
    values[keys_to_names[key]] = entry.value unless entry.expired?
  end
  values
end

- (Object) stats

Get the statistics from the memcached servers.



123
124
125
# File 'activesupport/lib/active_support/cache/mem_cache_store.rb', line 123

def stats
  @data.stats
end