Class: ActiveSupport::Cache::DalliStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::DalliStore
- Defined in:
- lib/active_support/cache/dalli_store.rb,
lib/active_support/cache/dalli_store23.rb
Overview
A cache store implementation which stores data in Memcached: www.danga.com/memcached/
DalliStore implements the Strategy::LocalCache strategy which implements an in memory cache inside of a block.
Defined Under Namespace
Modules: LocalCacheWithRaw
Constant Summary collapse
- ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/
Class Method Summary collapse
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the entire cache on all memcached servers.
-
#decrement(key, amount = 1) ⇒ Object
Decrement a cached value.
-
#delete(key, options = nil) ⇒ Object
:nodoc:.
-
#delete_matched(matcher, options = nil) ⇒ Object
:nodoc:.
-
#exist?(key, options = nil) ⇒ Boolean
:nodoc:.
-
#increment(key, amount = 1) ⇒ Object
Increment a cached value.
-
#initialize(*addresses) ⇒ DalliStore
constructor
Creates a new DalliStore object, with the given memcached server addresses.
-
#read(key, options = nil) ⇒ Object
Read an entry from the cache.
-
#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(key, value, options = nil) ⇒ Object
Writes a value to the cache.
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).
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_support/cache/dalli_store.rb', line 37 def initialize(*addresses) addresses = addresses.flatten = addresses. super() = .dup UNIVERSAL_OPTIONS.each{|name| .delete(name)} @data = self.class.build_mem_cache(*(addresses + [])) extend Strategy::LocalCache extend LocalCacheWithRaw end |
Class Method Details
Instance Method Details
#clear ⇒ Object
Clear the entire cache on all memcached servers. This method should be used with care when using a shared cache.
101 102 103 |
# File 'lib/active_support/cache/dalli_store.rb', line 101 def clear( = nil) @data.flush_all end |
#decrement(key, amount = 1) ⇒ 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 initialize that value to zero.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/active_support/cache/dalli_store.rb', line 87 def decrement(name, amount = 1, = nil) # :nodoc: = () initial = [:initial] || 0 expires_in = [:expires_in].to_i response = instrument(:decrement, name, :amount => amount) do @data.decr(escape_key(namespaced_key(name, )), amount, expires_in, initial) end rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.}") if logger nil end |
#delete(key, options = nil) ⇒ Object
:nodoc:
132 133 134 135 136 137 138 |
# File 'lib/active_support/cache/dalli_store23.rb', line 132 def delete(key, = nil) # :nodoc: super @data.delete(escape_key(key)) rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.}") false end |
#delete_matched(matcher, options = nil) ⇒ Object
:nodoc:
147 148 149 150 151 152 |
# File 'lib/active_support/cache/dalli_store23.rb', line 147 def delete_matched(matcher, = nil) # :nodoc: # don't do any local caching at present, just pass # through and let the error happen super raise "Not supported by Memcache" end |
#exist?(key, options = nil) ⇒ Boolean
:nodoc:
140 141 142 143 144 145 |
# File 'lib/active_support/cache/dalli_store23.rb', line 140 def exist?(key, = nil) # :nodoc: # Doesn't call super, cause exist? in memcache is in fact a read # But who cares? Reading is very fast anyway # Local cache is checked first, if it doesn't know then memcache itself is read from !read(key, ).nil? end |
#increment(key, amount = 1) ⇒ 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 initialize that value to zero.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_support/cache/dalli_store.rb', line 70 def increment(name, amount = 1, = nil) # :nodoc: = () initial = [:initial] || amount expires_in = [:expires_in].to_i response = instrument(:increment, name, :amount => amount) do @data.incr(escape_key(namespaced_key(name, )), amount, expires_in, initial) end rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.}") if logger nil end |
#read(key, options = nil) ⇒ Object
Read an entry from the cache.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/active_support/cache/dalli_store23.rb', line 104 def read(key, = nil) # :nodoc: super value = @data.get(escape_key(key), ) return nil if value.nil? value = && [:raw] ? value : Marshal.load(value) value rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.}") nil end |
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys. Options can be passed in the last argument.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/active_support/cache/dalli_store.rb', line 52 def read_multi(*names) = names. = () keys_to_names = names.inject({}){|map, name| map[escape_key(namespaced_key(name, ))] = name; map} raw_values = @data.get_multi(keys_to_names.keys, ) values = {} raw_values.each do |key, value| entry = deserialize_entry(value) values[keys_to_names[key]] = entry.value unless entry.expired? end values end |
#reset ⇒ Object
110 111 112 |
# File 'lib/active_support/cache/dalli_store.rb', line 110 def reset @data.reset end |
#stats ⇒ Object
Get the statistics from the memcached servers.
106 107 108 |
# File 'lib/active_support/cache/dalli_store.rb', line 106 def stats @data.stats end |
#write(key, value, options = nil) ⇒ Object
Writes a value to the cache.
Possible options:
-
:unless_exist
- set to true if you don’t want to update the cache if the key is already set. -
:expires_in
- the number of seconds that this value may stay in the cache. See ActiveSupport::Cache::Store#write for an example.
122 123 124 125 126 127 128 129 130 |
# File 'lib/active_support/cache/dalli_store23.rb', line 122 def write(key, value, = nil) super method = && [:unless_exist] ? :add : :set value = && [:raw] ? value.to_s : Marshal.dump(value) @data.send(method, escape_key(key), value, expires_in(), ) rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.}") false end |