Class: ActiveSupport::Cache::MemcachedStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::MemcachedStore
- 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
Defined Under Namespace
Modules: DupLocalCache Classes: Codec, DupLocalStore
Constant Summary collapse
- ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/n
Instance Attribute Summary collapse
-
#read_only ⇒ Object
Returns the value of attribute read_only.
-
#swallow_exceptions ⇒ Object
Returns the value of attribute swallow_exceptions.
Instance Method Summary collapse
- #append(name, value, options = nil) ⇒ Object
- #cas(name, options = nil) ⇒ Object
- #cas_multi(*names, **options) ⇒ Object
- #clear(options = nil) ⇒ Object
-
#decrement(name, amount = 1, options = nil) ⇒ Object
:nodoc:.
- #delete ⇒ Object
- #exist? ⇒ Boolean
-
#increment(name, amount = 1, options = nil) ⇒ Object
:nodoc:.
-
#initialize(*addresses, **options) ⇒ MemcachedStore
constructor
A new instance of MemcachedStore.
- #read_multi(*names) ⇒ Object
-
#reset ⇒ Object
:nodoc:.
- #stats ⇒ Object
- #write ⇒ Object
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, **) addresses = addresses.flatten [:codec] ||= Codec.new @swallow_exceptions = true @swallow_exceptions = .delete(:swallow_exceptions) if .key?(:swallow_exceptions) super() 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 = .dup servers = .delete(:servers) UNIVERSAL_OPTIONS.each { |name| .delete(name) } @connection = Memcached.new([*addresses, *servers], ) end end |
Instance Attribute Details
#read_only ⇒ Object
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_exceptions ⇒ Object
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, = nil) return true if read_only = () normalized_key = normalize_key(name, ) 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, = nil) = () key = normalize_key(name, ) handle_exceptions(return_value_on_error: false) do instrument(:cas, name, ) do @connection.cas(key, expiration()) do |raw_value| entry = deserialize_entry(raw_value) value = yield entry.value break true if read_only serialize_entry(Entry.new(value, **), ) 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, **) return if names.empty? = () keys_to_names = Hash[names.map { |name| [normalize_key(name, ), name] }] handle_exceptions(return_value_on_error: false) do instrument(:cas_multi, names, ) do @connection.cas(keys_to_names.keys, expiration()) 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, ), serialize_entry(Entry.new(value, **), )] 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( = nil) ActiveSupport::Notifications.instrument("cache_clear.active_support", || {}) 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, = nil) # :nodoc: = () handle_exceptions(return_value_on_error: nil) do instrument(:decrement, name, amount: amount) do @connection.decr(normalize_key(name, ), amount) end end end |
#delete ⇒ Object
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
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, = nil) # :nodoc: = () handle_exceptions(return_value_on_error: nil) do instrument(:increment, name, amount: amount) do @connection.incr(normalize_key(name, ), 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) = names. return {} if names.empty? = () keys_to_names = Hash[names.map { |name| [normalize_key(name, ), name] }] values = {} handle_exceptions(return_value_on_error: {}) do instrument(:read_multi, names, ) 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 |
#reset ⇒ Object
: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 |
#stats ⇒ Object
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 |
#write ⇒ Object
97 98 99 100 |
# File 'lib/active_support/cache/memcached_store.rb', line 97 def write(*) return true if read_only super end |