Class: ActiveSupport::Cache::SpymemcachedStore

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

Defined Under Namespace

Modules: LocalCacheWithRaw

Instance Method Summary collapse

Constructor Details

#initialize(*addresses) ⇒ SpymemcachedStore

Returns a new instance of SpymemcachedStore.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_support/cache/spymemcached_store.rb', line 8

def initialize(*addresses)
  addresses = addresses.flatten
  options = addresses.extract_options!
  super(options)

  unless [String, Spymemcached, NilClass].include?(addresses.first.class)
    raise ArgumentError, "First argument must be an empty array, an array of hosts or a Spymemcached instance."
  end
  @client = if addresses.first.is_a?(Spymemcached)
    addresses.first
  else
    mem_cache_options = options.dup
    UNIVERSAL_OPTIONS.each{|name| mem_cache_options.delete(name)}
    Spymemcached.new(addresses, mem_cache_options)
  end

  extend Strategy::LocalCache
  extend LocalCacheWithRaw
end

Instance Method Details

#clear(options = nil) ⇒ Object

Clear the entire cache. Be careful with this method since it could affect other processes if shared cache is being used.

The options hash is passed to the underlying cache implementation.

All implementations may not support this method.



85
86
87
88
89
90
# File 'lib/active_support/cache/spymemcached_store.rb', line 85

def clear(options = nil)
  @client.flush_all
rescue Spymemcached::Error => e
  logger.error("Spymemcached::Error (#{e}): #{e.message}") if logger
  nil
end

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

Decrement an integer value in the cache.

Options are passed to the underlying cache implementation.

All implementations may not support this method.



68
69
70
71
72
73
74
75
76
77
# File 'lib/active_support/cache/spymemcached_store.rb', line 68

def decrement(name, amount = 1, options = nil)
  options = merged_options(options)

  instrument(:decrement, name, :amount => amount) do
    @client.decr(name, amount)
  end
rescue Spymemcached::Error => e
  logger.error("Spymemcached::Error (#{e}): #{e.message}") if logger
  nil
end

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

Increment an integer value in the cache.

Options are passed to the underlying cache implementation.

All implementations may not support this method.



52
53
54
55
56
57
58
59
60
61
# File 'lib/active_support/cache/spymemcached_store.rb', line 52

def increment(name, amount = 1, options = nil)
  options = merged_options(options)

  instrument(:increment, name, :amount => amount) do
    @client.incr(name, amount)
  end
rescue Spymemcached::Error => e
  logger.error("Spymemcached::Error (#{e}): #{e.message}") if logger
  nil
end

#read_multi(*names) ⇒ Object

Read multiple values at once from the cache. Options can be passed in the last argument.

Some cache implementation may optimize this method.

Returns a hash mapping the names provided to the values found.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_support/cache/spymemcached_store.rb', line 34

def read_multi(*names)
  options = names.extract_options!
  options = merged_options(options)
  keys_to_names = Hash[names.map{|name| [namespaced_key(name, options), name]}]
  raw_values = @client.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

#statsObject

Get the statistics from the memcached servers.



93
94
95
# File 'lib/active_support/cache/spymemcached_store.rb', line 93

def stats
  @client.stats
end