Object
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).
# File lib/active_support/cache/dalli_store.rb, line 35 def initialize(*addresses) addresses = addresses.flatten options = addresses.extract_options! @options = 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
Clear any local cache
# File lib/active_support/cache/dalli_store.rb, line 204 def cleanup(options=nil) end
Clear the entire cache on all memcached servers. This method should be used with care when using a shared cache.
# File lib/active_support/cache/dalli_store.rb, line 193 def clear(options=nil) instrument(:clear, 'flushing all keys') do @data.flush_all end rescue Dalli::DalliError => e logger.error("DalliError: #{e.message}") if logger raise if @raise_errors nil end
Access the underlying Dalli::Client instance for access to get_multi, etc.
# File lib/active_support/cache/dalli_store.rb, line 54 def dalli @data end
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.
# File lib/active_support/cache/dalli_store.rb, line 177 def decrement(name, amount = 1, options=nil) options ||= {} name = expanded_key name initial = options.has_key?(:initial) ? options[:initial] : 0 expires_in = options[:expires_in] instrument(:decrement, name, :amount => amount) do @data.decr(name, amount, expires_in, initial) end rescue Dalli::DalliError => e logger.error("DalliError: #{e.message}") if logger raise if @raise_errors nil end
# File lib/active_support/cache/dalli_store.rb, line 117 def delete(name, options=nil) options ||= {} name = expanded_key name instrument(:delete, name, options) do |payload| delete_entry(name, options) end end
# File lib/active_support/cache/dalli_store.rb, line 109 def exist?(name, options=nil) options ||= {} name = expanded_key name log(:exist, name, options) !read_entry(name, options).nil? end
# File lib/active_support/cache/dalli_store.rb, line 58 def fetch(name, options=nil) options ||= {} name = expanded_key name if block_given? unless options[:force] entry = instrument(:read, name, options) do |payload| read_entry(name, options).tap do |result| if payload payload[:super_operation] = :fetch payload[:hit] = !!result end end end end if !entry.nil? instrument(:fetch_hit, name, options) { |payload| } entry else result = instrument(:generate, name, options) do |payload| yield end write(name, result, options) result end else read(name, options) end end
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.
# File lib/active_support/cache/dalli_store.rb, line 158 def increment(name, amount = 1, options=nil) options ||= {} name = expanded_key name initial = options.has_key?(:initial) ? options[:initial] : amount expires_in = options[:expires_in] instrument(:increment, name, :amount => amount) do @data.incr(name, amount, expires_in, initial) end rescue Dalli::DalliError => e logger.error("DalliError: #{e.message}") if logger raise if @raise_errors nil end
# File lib/active_support/cache/dalli_store.rb, line 216 def logger Dalli.logger end
# File lib/active_support/cache/dalli_store.rb, line 220 def logger=(new_logger) Dalli.logger = new_logger end
Silence the logger within a block.
# File lib/active_support/cache/dalli_store.rb, line 17 def mute previous_silence, @silence = defined?(@silence) && @silence, true yield ensure @silence = previous_silence end
# File lib/active_support/cache/dalli_store.rb, line 89 def read(name, options=nil) options ||= {} name = expanded_key name instrument(:read, name, options) do |payload| entry = read_entry(name, options) payload[:hit] = !!entry if payload entry end end
Reads multiple keys from the cache using a single call to the servers for all keys. Keys must be Strings.
# File lib/active_support/cache/dalli_store.rb, line 128 def read_multi(*names) names.extract_options! mapping = names.inject({}) { |memo, name| memo[expanded_key(name)] = name; memo } instrument(:read_multi, names) do results = {} if local_cache mapping.keys.each do |key| if value = local_cache.read_entry(key, options) 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, options) if local_cache memo end end end
# File lib/active_support/cache/dalli_store.rb, line 212 def reset @data.reset end
Silence the logger.
# File lib/active_support/cache/dalli_store.rb, line 11 def silence! @silence = true self end
Generated with the Darkfish Rdoc Generator 2.