Class: ActiveSupport::Cache::DalliStore

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/cache/dalli_store.rb

Constant Summary collapse

ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# File 'lib/active_support/cache/dalli_store.rb', line 36

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)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/active_support/cache/dalli_store.rb', line 8

def options
  @options
end

#silenceObject (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

#clear(options = nil) ⇒ Object

Clear the entire cache on all memcached servers. This method should be used with care when using a shared cache.



170
171
172
173
174
175
176
177
178
# File 'lib/active_support/cache/dalli_store.rb', line 170

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

#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.



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/active_support/cache/dalli_store.rb', line 154

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

#delete(name, options = nil) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/active_support/cache/dalli_store.rb', line 105

def delete(name, options=nil)
  options ||= {}
  name = expanded_key name

  log(:delete, name, options)
  delete_entry(name, options)
end

#exist?(name, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
# File 'lib/active_support/cache/dalli_store.rb', line 97

def exist?(name, options=nil)
  options ||= {}
  name = expanded_key name

  log(:exist, name, options)
  !read_entry(name, options).nil?
end

#fetch(name, options = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_support/cache/dalli_store.rb', line 50

def fetch(name, options=nil)
  options ||= {}
  name = expanded_key name

  if block_given?
    unless options[:force]
      entry = instrument(:read, name, options) do |payload|
        payload[:super_operation] = :fetch if payload
        read_entry(name, options)
      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(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.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/active_support/cache/dalli_store.rb', line 135

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

#loggerObject



189
190
191
# File 'lib/active_support/cache/dalli_store.rb', line 189

def logger
  Dalli.logger
end

#logger=(new_logger) ⇒ Object



193
194
195
# File 'lib/active_support/cache/dalli_store.rb', line 193

def logger=(new_logger)
  Dalli.logger = new_logger
end

#muteObject

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



77
78
79
80
81
82
83
84
85
86
# File 'lib/active_support/cache/dalli_store.rb', line 77

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

#read_multi(*names) ⇒ Object

Reads multiple keys from the cache using a single call to the servers for all keys. Keys must be Strings.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_support/cache/dalli_store.rb', line 115

def read_multi(*names)
  names.extract_options!
  names = names.flatten
  mapping = names.inject({}) { |memo, name| memo[escape(expanded_key(name))] = name; memo }
  instrument(:read_multi, names) do
    results = @data.get_multi(mapping.keys)
    results.inject({}) do |memo, (inner, _)|
      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

#resetObject



185
186
187
# File 'lib/active_support/cache/dalli_store.rb', line 185

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

#statsObject

Get the statistics from the memcached servers.



181
182
183
# File 'lib/active_support/cache/dalli_store.rb', line 181

def stats
  @data.stats
end

#write(name, value, options = nil) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/active_support/cache/dalli_store.rb', line 88

def write(name, value, options=nil)
  options ||= {}
  name = expanded_key name

  instrument(:write, name, options) do |payload|
    write_entry(name, value, options)
  end
end