Class: ActiveSupport::Cache::MemCacheStore

Inherits:
Store show all
Defined in:
lib/active_support/cache/mem_cache_store.rb

Direct Known Subclasses

CompressedMemCacheStore

Defined Under Namespace

Modules: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Store

#fetch, #threadsafe!

Constructor Details

#initialize(*addresses) ⇒ MemCacheStore

Returns a new instance of MemCacheStore.



16
17
18
19
20
21
22
# File 'lib/active_support/cache/mem_cache_store.rb', line 16

def initialize(*addresses)
  addresses = addresses.flatten
  options = addresses.extract_options!
  addresses = ["localhost"] if addresses.empty?
  @addresses = addresses
  @data = MemCache.new(addresses, options)
end

Instance Attribute Details

#addressesObject (readonly)

Returns the value of attribute addresses.



14
15
16
# File 'lib/active_support/cache/mem_cache_store.rb', line 14

def addresses
  @addresses
end

Instance Method Details

#clearObject



82
83
84
# File 'lib/active_support/cache/mem_cache_store.rb', line 82

def clear
  @data.flush_all
end

#decrement(key, amount = 1) ⇒ Object



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

def decrement(key, amount = 1)
  log("decrement", key, amount)
  
  response = data.decr(key, amount) 
  response == Response::NOT_FOUND ? nil : response
rescue MemCache::MemCacheError 
  nil
end

#delete(key, options = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/active_support/cache/mem_cache_store.rb', line 44

def delete(key, options = nil)
  super
  response = @data.delete(key, expires_in(options))
  response == Response::DELETED
rescue MemCache::MemCacheError => e
  logger.error("MemCacheError (#{e}): #{e.message}")
  false
end

#delete_matched(matcher, options = nil) ⇒ Object



77
78
79
80
# File 'lib/active_support/cache/mem_cache_store.rb', line 77

def delete_matched(matcher, options = nil)
  super
  raise "Not supported by Memcache"
end

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

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/active_support/cache/mem_cache_store.rb', line 53

def exist?(key, options = nil)
  # Doesn't call super, cause exist? in memcache is in fact a read
  # But who cares? Reading is very fast anyway
  !read(key, options).nil?
end

#increment(key, amount = 1) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/active_support/cache/mem_cache_store.rb', line 59

def increment(key, amount = 1)       
  log("incrementing", key, amount)
  
  response = @data.incr(key, amount)  
  response == Response::NOT_FOUND ? nil : response
rescue MemCache::MemCacheError 
  nil
end

#read(key, options = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_support/cache/mem_cache_store.rb', line 24

def read(key, options = nil)
  super
  @data.get(key, raw?(options))
rescue MemCache::MemCacheError => e
  logger.error("MemCacheError (#{e}): #{e.message}")
  nil
end

#statsObject



86
87
88
# File 'lib/active_support/cache/mem_cache_store.rb', line 86

def stats
  @data.stats
end

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

Set key = value. Pass :unless_exist => true if you don’t want to update the cache if the key is already set.



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

def write(key, value, options = nil)
  super
  method = options && options[:unless_exist] ? :add : :set
  response = @data.send(method, key, value, expires_in(options), raw?(options))
  response == Response::STORED
rescue MemCache::MemCacheError => e
  logger.error("MemCacheError (#{e}): #{e.message}")
  false
end