Class: Cash::Adapter::Memcached

Inherits:
Object
  • Object
show all
Defined in:
lib/cash/adapter/memcached.rb

Instance Method Summary collapse

Constructor Details

#initialize(repository, options = {}) ⇒ Memcached

Returns a new instance of Memcached.



7
8
9
10
11
# File 'lib/cash/adapter/memcached.rb', line 7

def initialize(repository, options = {})
  @repository = repository
  @logger = options[:logger]
  @default_ttl = options[:default_ttl] || raise(":default_ttl is a required option")   
end

Instance Method Details

#add(key, value, ttl = nil, raw = false) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/cash/adapter/memcached.rb', line 13

def add(key, value, ttl=nil, raw=false)
  wrap(key, not_stored) do
    logger.debug("Memcached add: #{key.inspect}") if debug_logger?
    @repository.add(key, raw ? value.to_s : value, ttl || @default_ttl, !raw)
    logger.debug("Memcached hit: #{key.inspect}") if debug_logger?
    stored
  end
end

#decr(key, value = 1) ⇒ Object



76
77
78
# File 'lib/cash/adapter/memcached.rb', line 76

def decr(key, value = 1)
  wrap(key) { @repository.decr(key, value) }
end

#delete(key) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/cash/adapter/memcached.rb', line 59

def delete(key)
  wrap(key, not_found) do
    logger.debug("Memcached delete: #{key.inspect}") if debug_logger?
    @repository.delete(key)
    logger.debug("Memcached hit: #{key.inspect}") if debug_logger?
    deleted
  end
end

#exception_classesObject



84
85
86
# File 'lib/cash/adapter/memcached.rb', line 84

def exception_classes
  ::Memcached::Error
end

#flush_allObject



80
81
82
# File 'lib/cash/adapter/memcached.rb', line 80

def flush_all
  @repository.flush
end

#get(key, raw = false) ⇒ Object

Wraps Memcached#get so that it doesn’t raise. This has the side-effect of preventing you from storing nil values.



24
25
26
27
28
29
30
31
# File 'lib/cash/adapter/memcached.rb', line 24

def get(key, raw=false)
  wrap(key) do
    logger.debug("Memcached get: #{key.inspect}") if debug_logger?
    value = wrap(key) { @repository.get(key, !raw) }
    logger.debug("Memcached hit: #{key.inspect}") if debug_logger?
    value
  end
end

#get_multi(*keys) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cash/adapter/memcached.rb', line 33

def get_multi(*keys)
  wrap(keys, {}) do
    begin
      keys.flatten!
      logger.debug("Memcached get_multi: #{keys.inspect}") if debug_logger?
      values = @repository.get(keys, true)
      logger.debug("Memcached hit: #{keys.inspect}") if debug_logger?
      values
    rescue TypeError
      log_error($!) if logger
      keys.each { |key| delete(key) }
      logger.debug("Memcached deleted: #{keys.inspect}") if debug_logger?
      {}
    end
  end
end

#get_server_for_key(key) ⇒ Object



68
69
70
# File 'lib/cash/adapter/memcached.rb', line 68

def get_server_for_key(key)
  wrap(key) { @repository.server_by_key(key) }
end

#incr(key, value = 1) ⇒ Object



72
73
74
# File 'lib/cash/adapter/memcached.rb', line 72

def incr(key, value = 1)
  wrap(key) { @repository.incr(key, value) }
end

#set(key, value, ttl = nil, raw = false) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/cash/adapter/memcached.rb', line 50

def set(key, value, ttl=nil, raw=false)
  wrap(key, not_stored) do
    logger.debug("Memcached set: #{key.inspect}") if debug_logger?
    @repository.set(key, raw ? value.to_s : value, ttl || @default_ttl, !raw)
    logger.debug("Memcached hit: #{key.inspect}") if debug_logger?
    stored
  end
end