Class: ActiveSupport::Cache::MemcachedStore

Inherits:
Store
  • Object
show all
Includes:
DupLocalCache, Strategy::LocalCache
Defined in:
lib/active_support/cache/memcached_store.rb

Overview

A cache store implementation which stores data in Memcached: memcached.org/

MemcachedStore uses memcached gem as backend to connect to Memcached server.

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

Direct Known Subclasses

MemcachedSnappyStore

Defined Under Namespace

Modules: DupLocalCache Classes: Codec, DupLocalStore

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*addresses, **options) ⇒ MemcachedStore

Returns a new instance of MemcachedStore.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_support/cache/memcached_store.rb', line 64

def initialize(*addresses, **options)
  addresses = addresses.flatten
  options[:codec] ||= Codec.new
  @swallow_exceptions = true
  @swallow_exceptions = options.delete(:swallow_exceptions) if options.key?(:swallow_exceptions)

  super(options)

  if addresses.first.is_a?(Memcached)
    @connection = addresses.first
    raise "Memcached::Rails is no longer supported, "\
          "use a Memcached instance instead" if @connection.is_a?(Memcached::Rails)
  else
    mem_cache_options = options.dup
    servers = mem_cache_options.delete(:servers)
    UNIVERSAL_OPTIONS.each { |name| mem_cache_options.delete(name) }
    @connection = Memcached.new([*addresses, *servers], mem_cache_options)
  end
end

Instance Attribute Details

#read_onlyObject

Returns the value of attribute read_only.



60
61
62
# File 'lib/active_support/cache/memcached_store.rb', line 60

def read_only
  @read_only
end

#swallow_exceptionsObject

Returns the value of attribute swallow_exceptions.



60
61
62
# File 'lib/active_support/cache/memcached_store.rb', line 60

def swallow_exceptions
  @swallow_exceptions
end

Instance Method Details

#append(name, value, options = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_support/cache/memcached_store.rb', line 84

def append(name, value, options = nil)
  return true if read_only
  options = merged_options(options)
  normalized_key = normalize_key(name, options)

  handle_exceptions(return_value_on_error: nil, on_miss: false, miss_exceptions: [Memcached::NotStored]) do
    instrument(:append, name) do
      @connection.append(normalized_key, value)
    end
    true
  end
end

#cas(name, options = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/active_support/cache/memcached_store.rb', line 128

def cas(name, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  handle_exceptions(return_value_on_error: false) do
    instrument(:cas, name, options) do
      @connection.cas(key, expiration(options)) do |raw_value|
        entry = deserialize_entry(raw_value)
        value = yield entry.value
        break true if read_only
        serialize_entry(Entry.new(value, **options), options)
      end
    end
    true
  end
end

#cas_multi(*names, **options) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/active_support/cache/memcached_store.rb', line 145

def cas_multi(*names, **options)
  return if names.empty?

  options = merged_options(options)
  keys_to_names = Hash[names.map { |name| [normalize_key(name, options), name] }]

  handle_exceptions(return_value_on_error: false) do
    instrument(:cas_multi, names, options) do
      @connection.cas(keys_to_names.keys, expiration(options)) do |raw_values|
        values = {}

        raw_values.each do |key, raw_value|
          entry = deserialize_entry(raw_value)
          values[keys_to_names[key]] = entry.value unless entry.expired?
        end

        values = yield values

        break true if read_only

        serialized_values = values.map do |name, value|
          [normalize_key(name, options), serialize_entry(Entry.new(value, **options), options)]
        end

        Hash[serialized_values]
      end
      true
    end
  end
end

#clear(options = nil) ⇒ Object



194
195
196
197
198
# File 'lib/active_support/cache/memcached_store.rb', line 194

def clear(options = nil)
  ActiveSupport::Notifications.instrument("cache_clear.active_support", options || {}) do
    @connection.flush
  end
end

#decrement(name, amount = 1, options = nil) ⇒ Object

:nodoc:



185
186
187
188
189
190
191
192
# File 'lib/active_support/cache/memcached_store.rb', line 185

def decrement(name, amount = 1, options = nil) # :nodoc:
  options = merged_options(options)
  handle_exceptions(return_value_on_error: nil) do
    instrument(:decrement, name, amount: amount) do
      @connection.decr(normalize_key(name, options), amount)
    end
  end
end

#deleteObject



102
103
104
105
# File 'lib/active_support/cache/memcached_store.rb', line 102

def delete(*)
  return true if read_only
  super
end

#exist?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/active_support/cache/memcached_store.rb', line 206

def exist?(*)
  !!super
end

#increment(name, amount = 1, options = nil) ⇒ Object

:nodoc:



176
177
178
179
180
181
182
183
# File 'lib/active_support/cache/memcached_store.rb', line 176

def increment(name, amount = 1, options = nil) # :nodoc:
  options = merged_options(options)
  handle_exceptions(return_value_on_error: nil) do
    instrument(:increment, name, amount: amount) do
      @connection.incr(normalize_key(name, options), amount)
    end
  end
end

#read_multi(*names) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_support/cache/memcached_store.rb', line 107

def read_multi(*names)
  options = names.extract_options!
  return {} if names.empty?

  options = merged_options(options)
  keys_to_names = Hash[names.map { |name| [normalize_key(name, options), name] }]
  values = {}

  handle_exceptions(return_value_on_error: {}) do
    instrument(:read_multi, names, options) do
      if raw_values = @connection.get(keys_to_names.keys)
        raw_values.each do |key, value|
          entry = deserialize_entry(value)
          values[keys_to_names[key]] = entry.value unless entry.expired?
        end
      end
    end
    values
  end
end

#resetObject

:nodoc:



210
211
212
213
214
# File 'lib/active_support/cache/memcached_store.rb', line 210

def reset #:nodoc:
  handle_exceptions(return_value_on_error: false) do
    @connection.reset
  end
end

#statsObject



200
201
202
203
204
# File 'lib/active_support/cache/memcached_store.rb', line 200

def stats
  ActiveSupport::Notifications.instrument("cache_stats.active_support") do
    @connection.stats
  end
end

#writeObject



97
98
99
100
# File 'lib/active_support/cache/memcached_store.rb', line 97

def write(*)
  return true if read_only
  super
end