Class: ActiveSupport::Cache::LibmemcachedStore

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

Overview

Store using memcached gem as client

Global options can be passed to be applied to each method by default. Supported options are

  • :compress : if set to true, data will be compress before stored

  • :compress_threshold : specify the threshold at which to compress

value, default is 4K

  • :namespace : prepend each key with this value for simple namespacing

  • :expires_in : default TTL in seconds for each. Default value is 0, i.e. forever

Specific value can be passed per key with write and fetch command.

Options can also be passed direclty to the memcache client, via the :client option. For example, if you want to use pipelining, you can use :client => { :no_block => true }

Defined Under Namespace

Classes: MemcachedWithFlags

Constant Summary collapse

DEFAULT_CLIENT_OPTIONS =
{ distribution: :consistent_ketama, binary_protocol: true, default_ttl: 0 }
ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/n
DEFAULT_COMPRESS_THRESHOLD =
4096
FLAG_COMPRESSED =
0x2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*addresses) ⇒ LibmemcachedStore

Returns a new instance of LibmemcachedStore.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_support/cache/libmemcached_store.rb', line 54

def initialize(*addresses)
  addresses.flatten!
  options = addresses.extract_options!
  client_options = options.delete(:client) || {}
  if options[:namespace]
    client_options[:prefix_key] = options.delete(:namespace)
    client_options[:prefix_delimiter] = ':'
    @namespace_length = client_options[:prefix_key].length + 1
  else
    @namespace_length = 0
  end
  client_options[:default_ttl] = options.delete(:expires_in).to_i if options[:expires_in]

  @options = options.reverse_merge(compress_threshold: DEFAULT_COMPRESS_THRESHOLD)
  @addresses = addresses
  @cache = MemcachedWithFlags.new(@addresses, client_options.reverse_merge(DEFAULT_CLIENT_OPTIONS))
end

Instance Attribute Details

#addressesObject (readonly)

Returns the value of attribute addresses.



30
31
32
# File 'lib/active_support/cache/libmemcached_store.rb', line 30

def addresses
  @addresses
end

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'lib/active_support/cache/libmemcached_store.rb', line 37

def options
  @options
end

#silenceObject (readonly) Also known as: silence?

Returns the value of attribute silence.



37
38
39
# File 'lib/active_support/cache/libmemcached_store.rb', line 37

def silence
  @silence
end

Instance Method Details

#clear(options = nil) ⇒ Object



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

def clear(options = nil)
  @cache.flush
end

#decrement(key, amount = 1, options = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/active_support/cache/libmemcached_store.rb', line 150

def decrement(key, amount = 1, options = nil)
  key = expanded_key(key)
  instrument(:decrement, key, amount: amount) do
    @cache.decr(escape_and_normalize(key), amount)
  end
rescue Memcached::NotFound
  nil
rescue Memcached::Error => e
  log_error(e)
  nil
end

#delete(key, options = nil) ⇒ Object



117
118
119
120
121
122
# File 'lib/active_support/cache/libmemcached_store.rb', line 117

def delete(key, options = nil)
  key = expanded_key(key)
  instrument(:delete, key) do |payload|
    delete_entry(key, options)
  end
end

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

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/active_support/cache/libmemcached_store.rb', line 124

def exist?(key, options = nil)
  key = expanded_key(key)
  instrument(:exist?, key) do |payload|
    if @cache.respond_to?(:exist)
      @cache.exist(escape_and_normalize(key))
      true
    else
      read_entry(key, options) != nil
    end
  end
rescue Memcached::NotFound
  false
end

#fetch(key, options = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/active_support/cache/libmemcached_store.rb', line 72

def fetch(key, options = nil)
  if block_given?
    key = expanded_key(key)
    unless options && options[:force]
      entry = instrument(:read, key, options) do |payload|
        read_entry(key, options).tap do |result|
          if payload
            payload[:super_operation] = :fetch
            payload[:hit] = !!result
          end
        end
      end
    end

    if entry.nil?
      result = instrument(:generate, key, options) do |payload|
        yield
      end
      write_entry(key, result, options)
      result
    else
      instrument(:fetch_hit, key, options) { |payload| }
      entry
    end
  else
    read(key, options)
  end
end

#increment(key, amount = 1, options = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/active_support/cache/libmemcached_store.rb', line 138

def increment(key, amount = 1, options = nil)
  key = expanded_key(key)
  instrument(:increment, key, amount: amount) do
    @cache.incr(escape_and_normalize(key), amount)
  end
rescue Memcached::NotFound
  nil
rescue Memcached::Error => e
  log_error(e)
  nil
end

#muteObject

Silence the logger within a block.



47
48
49
50
51
52
# File 'lib/active_support/cache/libmemcached_store.rb', line 47

def mute
  previous_silence, @silence = defined?(@silence) && @silence, true
  yield
ensure
  @silence = previous_silence
end

#read(key, options = nil) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/active_support/cache/libmemcached_store.rb', line 101

def read(key, options = nil)
  key = expanded_key(key)
  instrument(:read, key, options) do |payload|
    entry = read_entry(key, options)
    payload[:hit] = !!entry if payload
    entry
  end
end

#read_multi(*names) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/active_support/cache/libmemcached_store.rb', line 162

def read_multi(*names)
  names.flatten!
  options = names.extract_options!

  return {} if names.empty?

  mapping = Hash[names.map {|name| [escape_and_normalize(expanded_key(name)), name] }]
  raw_values, flags = @cache.get(mapping.keys, false, true)

  values = {}
  raw_values.each do |key, value|
    values[mapping[key]] = deserialize(value, options[:raw], flags[key])
  end
  values
rescue Memcached::Error => e
  log_error(e)
  {}
end

#silence!Object

Silence the logger.



41
42
43
44
# File 'lib/active_support/cache/libmemcached_store.rb', line 41

def silence!
  @silence = true
  self
end

#statsObject



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

def stats
  @cache.stats
end

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



110
111
112
113
114
115
# File 'lib/active_support/cache/libmemcached_store.rb', line 110

def write(key, value, options = nil)
  key = expanded_key(key)
  instrument(:write, key, options) do |payload|
    write_entry(key, value, options)
  end
end