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 Method Summary collapse
-
#clear(options = nil) ⇒ Object
Clear the entire cache on all memcached servers.
-
#decrement(name, amount = 1, options = {}) ⇒ Object
Decrement a cached value.
- #delete(name, options = {}) ⇒ Object
- #exist?(name, options = {}) ⇒ Boolean
- #fetch(name, options = {}) ⇒ Object
-
#increment(name, amount = 1, options = {}) ⇒ Object
Increment a cached value.
-
#initialize(*addresses) ⇒ DalliStore
constructor
Creates a new DalliStore object, with the given memcached server addresses.
- #read(name, options = {}) ⇒ Object
-
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys.
- #reset ⇒ Object
-
#stats ⇒ Object
Get the statistics from the memcached servers.
- #write(name, value, options = {}) ⇒ 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).
20 21 22 23 24 25 26 |
# File 'lib/active_support/cache/dalli_store.rb', line 20 def initialize(*addresses) addresses = addresses.flatten = addresses. [:compression] = .delete(:compress) || [:compression] addresses << 'localhost:11211' if addresses.empty? @data = Dalli::Client.new(addresses, ) end |
Instance Method Details
#clear(options = nil) ⇒ Object
Clear the entire cache on all memcached servers. This method should be used with care when using a shared cache.
126 127 128 |
# File 'lib/active_support/cache/dalli_store.rb', line 126 def clear(=nil) @data.flush_all end |
#decrement(name, amount = 1, options = {}) ⇒ 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.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/active_support/cache/dalli_store.rb', line 113 def decrement(name, amount = 1, ={}) # :nodoc: 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 nil end |
#delete(name, options = {}) ⇒ Object
70 71 72 |
# File 'lib/active_support/cache/dalli_store.rb', line 70 def delete(name, ={}) @data.delete(name) end |
#exist?(name, options = {}) ⇒ Boolean
66 67 68 |
# File 'lib/active_support/cache/dalli_store.rb', line 66 def exist?(name, ={}) !!read_entry(name, ) end |
#fetch(name, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_support/cache/dalli_store.rb', line 28 def fetch(name, ={}) if block_given? unless [:force] entry = instrument(:read, name, ) do |payload| payload[:super_operation] = :fetch if payload read_entry(name, ) end end if entry 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 = {}) ⇒ 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.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/active_support/cache/dalli_store.rb', line 97 def increment(name, amount = 1, ={}) # :nodoc: 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 nil end |
#read(name, options = {}) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/active_support/cache/dalli_store.rb', line 52 def read(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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_support/cache/dalli_store.rb', line 76 def read_multi(*names) names. names = names.flatten mapping = names.inject({}) { |memo, name| memo[escape(name)] = name; memo } instrument(:read_multi, names) do results = @data.get_multi(mapping.keys) results.inject({}) do |memo, (inner, value)| entry = results[inner] # NB Backwards data compatibility, to be removed at some point memo[mapping[inner]] = (entry.is_a?(ActiveSupport::Cache::Entry) ? entry.value : entry) memo end end end |
#reset ⇒ Object
135 136 137 |
# File 'lib/active_support/cache/dalli_store.rb', line 135 def reset @data.reset end |
#stats ⇒ Object
Get the statistics from the memcached servers.
131 132 133 |
# File 'lib/active_support/cache/dalli_store.rb', line 131 def stats @data.stats end |
#write(name, value, options = {}) ⇒ Object
60 61 62 63 64 |
# File 'lib/active_support/cache/dalli_store.rb', line 60 def write(name, value, ={}) instrument(:write, name, ) do |payload| write_entry(name, value, ) end end |