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
83
84
85
86
87
88
# 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)

  if options.key?(:coder)
    raise ArgumentError, "ActiveSupport::Cache::MemcachedStore doesn't support custom coders"
  end

  # We don't use a coder, so we set it to nil so Active Support don't think we're using
  # a deprecated one.
  super(options.merge(coder: nil))

  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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_support/cache/memcached_store.rb', line 90

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/active_support/cache/memcached_store.rb', line 134

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

  success = 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
        payload = serialize_entry(Entry.new(value, **options), options)
      end
    end
    true
  end

  if success
    local_cache.write_entry(key, payload) if local_cache
  else
    local_cache.delete_entry(key) if local_cache
  end

  success
end

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/active_support/cache/memcached_store.rb', line 160

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] }]

  sent_payloads = nil

  handle_exceptions(return_value_on_error: false) do
    instrument(:cas_multi, names, options) do
      written_payloads = @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

        sent_payloads = Hash[serialized_values]
      end

      if local_cache && sent_payloads
        sent_payloads.each_key do |key|
          if written_payloads.key?(key)
            local_cache.write_entry(key, written_payloads[key])
          else
            local_cache.delete_entry(key)
          end
        end
      end

      true
    end
  end
end

#clear(options = nil) ⇒ Object



222
223
224
225
226
# File 'lib/active_support/cache/memcached_store.rb', line 222

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:



213
214
215
216
217
218
219
220
# File 'lib/active_support/cache/memcached_store.rb', line 213

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



108
109
110
111
# File 'lib/active_support/cache/memcached_store.rb', line 108

def delete(*)
  return true if read_only
  super
end

#exist?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/active_support/cache/memcached_store.rb', line 234

def exist?(*)
  !!super
end

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

:nodoc:



204
205
206
207
208
209
210
211
# File 'lib/active_support/cache/memcached_store.rb', line 204

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/active_support/cache/memcached_store.rb', line 113

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:



238
239
240
241
242
# File 'lib/active_support/cache/memcached_store.rb', line 238

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

#statsObject



228
229
230
231
232
# File 'lib/active_support/cache/memcached_store.rb', line 228

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

#writeObject



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

def write(*)
  return true if read_only
  super
end