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
- ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/- RAW =
{ :raw => true }
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Object) clear
Clear the entire cache on all memcached servers.
-
- (Object) decrement(key, amount = 1)
Decrement a cached value.
-
- (Object) delete(key, options = nil)
:nodoc:.
-
- (Object) delete_matched(matcher, options = nil)
:nodoc:.
-
- (Boolean) exist?(key, options = nil)
:nodoc:.
-
- (Object) increment(key, amount = 1)
Increment a cached value.
-
- (DalliStore) initialize(*addresses)
constructor
Creates a new DalliStore object, with the given memcached server addresses.
-
- (Object) read(key, options = nil)
Read an entry from the cache.
-
- (Object) read_multi(*names)
Reads multiple keys from the cache using a single call to the servers for all keys.
- - (Object) reset
-
- (Object) stats
Get the statistics from the memcached servers.
-
- (Object) write(key, value, options = nil)
Writes a value to the cache.
Constructor Details
- (DalliStore) initialize(*addresses)
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).
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_support/cache/dalli_store.rb', line 38 def initialize(*addresses) addresses = addresses.flatten = addresses. = .dup @namespace = .delete(:namespace) @expires_in = [:expires_in] @data = self.class.build_mem_cache(*(addresses + [])) extend Strategy::LocalCache end |
Class Method Details
+ (Object) build_mem_cache(*addresses)
22 23 24 25 26 27 |
# File 'lib/active_support/cache/dalli_store.rb', line 22 def self.build_mem_cache(*addresses) addresses = addresses.flatten = addresses. addresses = ["localhost"] if addresses.empty? Dalli::Client.new(addresses, ) end |
Instance Method Details
- (Object) clear
Clear the entire cache on all memcached servers. This method should be used with care when using a shared cache.
102 103 104 |
# File 'lib/active_support/cache/dalli_store.rb', line 102 def clear @data.flush_all end |
- (Object) decrement(key, amount = 1)
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.
88 89 90 91 92 93 94 |
# File 'lib/active_support/cache/dalli_store.rb', line 88 def decrement(key, amount = 1) # :nodoc: log("decrement", key, amount) @data.decr(escape_key(key), amount) rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") if logger nil end |
- (Object) delete(key, options = nil)
:nodoc:
97 98 99 100 101 102 103 |
# File 'lib/active_support/cache/dalli_store23.rb', line 97 def delete(key, = nil) # :nodoc: super @data.delete(escape_key(key)) rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") false end |
- (Object) delete_matched(matcher, options = nil)
:nodoc:
136 137 138 139 140 141 |
# File 'lib/active_support/cache/dalli_store23.rb', line 136 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 |
- (Boolean) exist?(key, options = nil)
:nodoc:
105 106 107 108 109 110 |
# File 'lib/active_support/cache/dalli_store23.rb', line 105 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 |
- (Object) increment(key, amount = 1)
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.
71 72 73 74 75 76 77 |
# File 'lib/active_support/cache/dalli_store.rb', line 71 def increment(key, amount = 1) # :nodoc: log("incrementing", key, amount) @data.incr(escape_key(key), amount) rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") if logger nil end |
- (Object) read(key, options = nil)
Read an entry from the cache.
72 73 74 75 76 77 78 |
# File 'lib/active_support/cache/dalli_store23.rb', line 72 def read(key, = nil) # :nodoc: super @data.get(escape_key(key), ) rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") nil end |
- (Object) read_multi(*names)
Reads multiple keys from the cache using a single call to the servers for all keys. Options can be passed in the last argument.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/active_support/cache/dalli_store.rb', line 53 def read_multi(*names) = nil = names.pop if names.last.is_a?(Hash) keys_to_names = names.inject({}){|map, name| map[escape_key(name)] = name; map} cache_keys = {} # map keys to servers names.each do |key| cache_key = escape_key key cache_keys[cache_key] = key end values = @data.get_multi(keys_to_names.keys, ) results = {} values.each do |key, value| results[cache_keys[key]] = value end results end |
- (Object) reset
111 112 113 |
# File 'lib/active_support/cache/dalli_store.rb', line 111 def reset @data.reset end |
- (Object) stats
Get the statistics from the memcached servers.
107 108 109 |
# File 'lib/active_support/cache/dalli_store.rb', line 107 def stats @data.stats end |
- (Object) write(key, value, options = nil)
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.
87 88 89 90 91 92 93 94 95 |
# File 'lib/active_support/cache/dalli_store23.rb', line 87 def write(key, value, = nil) super value = value.to_s if && [:raw] method = && [:unless_exist] ? :add : :set @data.send(method, escape_key(key), value, expires_in(), ) rescue Dalli::DalliError => e logger.error("DalliError: #{e.}") false end |