Class: Cash::Adapter::Redis

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Redis.



5
6
7
8
9
# File 'lib/cash/adapter/redis.rb', line 5

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



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cash/adapter/redis.rb', line 11

def add(key, value, ttl=nil, raw = false)
  wrap(key, not_stored) do
    logger.debug("Redis add: #{key.inspect}") if debug_logger?
    value = dump(value) unless raw
    # TODO: make transactional
    result = @repository.setnx(key, value)
    @repository.expires(key, ttl || @default_ttl) if 1 == result
    logger.debug("Redis hit: #{key.inspect}") if debug_logger?
    1 == result ? stored : not_stored
  end
end

#decr(key, value = 1) ⇒ Object



92
93
94
95
96
# File 'lib/cash/adapter/redis.rb', line 92

def decr(key, value = 1)
  if wrap(nil) { @repository.exists(key) }
    wrap(key) { @repository.decrby(key, value).to_i }
  end
end

#delete(key) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/cash/adapter/redis.rb', line 67

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

#exception_classesObject



102
103
104
# File 'lib/cash/adapter/redis.rb', line 102

def exception_classes
  [Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EBADF, Errno::EINVAL]
end

#flush_allObject



98
99
100
# File 'lib/cash/adapter/redis.rb', line 98

def flush_all
  @repository.flushall
end

#get(key, raw = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cash/adapter/redis.rb', line 23

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

#get_multi(*keys) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cash/adapter/redis.rb', line 37

def get_multi(*keys)
  wrap(keys, {}) do
    keys.flatten!
    logger.debug("Redis get_multi: #{keys.inspect}") if debug_logger?
    
    # Values are returned as an array. Convert them to a hash of matches, dropping anything
    # that doesn't have a match.
    values = @repository.mget(*keys)
    result = {}
    keys.each_with_index{ |key, i| result[key] = load(values[i]) if values[i] }
    
    if result.any?
      logger.debug("Redis hit: #{keys.inspect}") if debug_logger?
    else
      logger.debug("Redis miss: #{keys.inspect}") if debug_logger?
    end
    result
  end
end

#get_server_for_key(key) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/cash/adapter/redis.rb', line 76

def get_server_for_key(key)
  wrap(key) do
    # Redis::Distributed has a node_for method. 
    client = @repository.respond_to?(:node_for) ? @repository.node_for(key) : @repository.client
    client.id
  end
end

#incr(key, value = 1) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/cash/adapter/redis.rb', line 84

def incr(key, value = 1)
  # Redis always answeres positively to incr/decr but memcache does not and waits for the key
  # to be added in a separate operation.
  if wrap(nil) { @repository.exists(key) }
    wrap(key) { @repository.incrby(key, value).to_i }
  end
end

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



57
58
59
60
61
62
63
64
65
# File 'lib/cash/adapter/redis.rb', line 57

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