Class: ActiveSupport::Cache::DalliStore
- Inherits:
-
Object
- Object
- ActiveSupport::Cache::DalliStore
- Defined in:
- lib/active_support/cache/dalli_store.rb
Constant Summary collapse
- ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#silence ⇒ Object
(also: #silence?)
readonly
Returns the value of attribute silence.
Instance Method Summary collapse
-
#cleanup(options = nil) ⇒ Object
Clear any local cache.
-
#clear(options = nil) ⇒ Object
Clear the entire cache on all memcached servers.
-
#dalli ⇒ Object
Access the underlying Dalli::Client instance for access to get_multi, etc.
-
#decrement(name, amount = 1, options = nil) ⇒ Object
Decrement a cached value.
- #delete(name, options = nil) ⇒ Object
- #exist?(name, options = nil) ⇒ Boolean
- #fetch(name, options = nil) ⇒ Object
-
#increment(name, amount = 1, options = nil) ⇒ Object
Increment a cached value.
-
#initialize(*addresses) ⇒ DalliStore
constructor
Creates a new DalliStore object, with the given memcached server addresses.
- #logger ⇒ Object
- #logger=(new_logger) ⇒ Object
-
#mute ⇒ Object
Silence the logger within a block.
- #read(name, options = nil) ⇒ Object
-
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys.
- #reset ⇒ Object
-
#silence! ⇒ Object
Silence the logger.
-
#stats ⇒ Object
Get the statistics from the memcached servers.
- #write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize(*addresses) ⇒ DalliStore
Creates a new DalliStore 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::DalliStore.new("localhost", "server-downstairs.localnetwork:8229")
If no addresses are specified, then DalliStore will connect to localhost port 11211 (the default memcached port).
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_support/cache/dalli_store.rb', line 36 def initialize(*addresses) addresses = addresses.flatten = addresses. @options = .dup @options[:compress] ||= @options[:compression] @raise_errors = !!@options[:raise_errors] servers = if addresses.empty? nil # use the default from Dalli::Client else addresses end @data = Dalli::Client.new(servers, @options) extend Strategy::LocalCache end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/active_support/cache/dalli_store.rb', line 8 def @options end |
#silence ⇒ Object (readonly) Also known as: silence?
Returns the value of attribute silence.
8 9 10 |
# File 'lib/active_support/cache/dalli_store.rb', line 8 def silence @silence end |
Instance Method Details
#cleanup(options = nil) ⇒ Object
Clear any local cache
205 206 |
# File 'lib/active_support/cache/dalli_store.rb', line 205 def cleanup(=nil) end |
#clear(options = nil) ⇒ Object
Clear the entire cache on all memcached servers. This method should be used with care when using a shared cache.
194 195 196 197 198 199 200 201 202 |
# File 'lib/active_support/cache/dalli_store.rb', line 194 def clear(=nil) instrument(:clear, 'flushing all keys') do @data.flush_all end rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") if logger raise if @raise_errors nil end |
#dalli ⇒ Object
Access the underlying Dalli::Client instance for access to get_multi, etc.
55 56 57 |
# File 'lib/active_support/cache/dalli_store.rb', line 55 def dalli @data end |
#decrement(name, amount = 1, options = nil) ⇒ Object
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 fail. :initial defaults to zero, as if the counter was initially zero. memcached counters cannot hold negative values.
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/active_support/cache/dalli_store.rb', line 178 def decrement(name, amount = 1, =nil) ||= {} name = name initial = .has_key?(:initial) ? [:initial] : 0 expires_in = [:expires_in] instrument(:decrement, name, :amount => amount) do @data.decr(name, amount, expires_in, initial) end rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") if logger raise if @raise_errors nil end |
#delete(name, options = nil) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/active_support/cache/dalli_store.rb', line 118 def delete(name, =nil) ||= {} name = name instrument(:delete, name, ) do |payload| delete_entry(name, ) end end |
#exist?(name, options = nil) ⇒ Boolean
110 111 112 113 114 115 116 |
# File 'lib/active_support/cache/dalli_store.rb', line 110 def exist?(name, =nil) ||= {} name = name log(:exist, name, ) !read_entry(name, ).nil? end |
#fetch(name, options = nil) ⇒ Object
59 60 61 62 63 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/dalli_store.rb', line 59 def fetch(name, =nil) ||= {} name = name if block_given? unless [:force] entry = instrument(:read, name, ) do |payload| read_entry(name, ).tap do |result| if payload payload[:super_operation] = :fetch payload[:hit] = !!result end end end end if !entry.nil? instrument(:fetch_hit, name, ) { |payload| } entry else result = instrument(:generate, name, ) do |payload| yield end write(name, result, ) result end else read(name, ) end end |
#increment(name, amount = 1, options = nil) ⇒ Object
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 fail. :initial defaults to the amount passed in, as if the counter was initially zero. memcached counters cannot hold negative values.
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/active_support/cache/dalli_store.rb', line 159 def increment(name, amount = 1, =nil) ||= {} name = name initial = .has_key?(:initial) ? [:initial] : amount expires_in = [:expires_in] instrument(:increment, name, :amount => amount) do @data.incr(name, amount, expires_in, initial) end rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") if logger raise if @raise_errors nil end |
#logger ⇒ Object
217 218 219 |
# File 'lib/active_support/cache/dalli_store.rb', line 217 def logger Dalli.logger end |
#logger=(new_logger) ⇒ Object
221 222 223 |
# File 'lib/active_support/cache/dalli_store.rb', line 221 def logger=(new_logger) Dalli.logger = new_logger end |
#mute ⇒ Object
Silence the logger within a block.
18 19 20 21 22 23 |
# File 'lib/active_support/cache/dalli_store.rb', line 18 def mute previous_silence, @silence = defined?(@silence) && @silence, true yield ensure @silence = previous_silence end |
#read(name, options = nil) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_support/cache/dalli_store.rb', line 90 def read(name, =nil) ||= {} name = name instrument(:read, name, ) do |payload| entry = read_entry(name, ) payload[:hit] = !!entry if payload entry end end |
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys. Keys must be Strings.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_support/cache/dalli_store.rb', line 129 def read_multi(*names) names. mapping = names.inject({}) { |memo, name| memo[(name)] = name; memo } instrument(:read_multi, names) do results = {} if local_cache mapping.keys.each do |key| if value = local_cache.read_entry(key, ) results[key] = value end end end results.merge!(@data.get_multi(mapping.keys - results.keys)) results.inject({}) do |memo, (inner, _)| entry = results[inner] # NB Backwards data compatibility, to be removed at some point value = (entry.is_a?(ActiveSupport::Cache::Entry) ? entry.value : entry) memo[mapping[inner]] = value local_cache.write_entry(inner, value, ) if local_cache memo end end end |
#reset ⇒ Object
213 214 215 |
# File 'lib/active_support/cache/dalli_store.rb', line 213 def reset @data.reset end |
#silence! ⇒ Object
Silence the logger.
12 13 14 15 |
# File 'lib/active_support/cache/dalli_store.rb', line 12 def silence! @silence = true self end |
#stats ⇒ Object
Get the statistics from the memcached servers.
209 210 211 |
# File 'lib/active_support/cache/dalli_store.rb', line 209 def stats @data.stats end |
#write(name, value, options = nil) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/active_support/cache/dalli_store.rb', line 101 def write(name, value, =nil) ||= {} name = name instrument(:write, name, ) do |payload| write_entry(name, value, ) end end |