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 83 84 85 86 87 88 |
# 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) if .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(.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 = .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
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, = 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
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, = nil) = () key = normalize_key(name, ) payload = nil success = 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 payload = serialize_entry(Entry.new(value, **), ) 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, **) return if names.empty? = () keys_to_names = Hash[names.map { |name| [normalize_key(name, ), name] }] sent_payloads = nil handle_exceptions(return_value_on_error: false) do instrument(:cas_multi, names, ) do written_payloads = @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 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( = nil) ActiveSupport::Notifications.instrument("cache_clear.active_support", || {}) 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, = 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
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
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, = 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
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) = 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:
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 |
#stats ⇒ Object
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 |
#write ⇒ Object
103 104 105 106 |
# File 'lib/active_support/cache/memcached_store.rb', line 103 def write(*) return true if read_only super end |