Class: ActiveSupport::Cache::RedisStore

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

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ RedisStore

Returns a new instance of RedisStore.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_support/cache/redis_store.rb', line 13

def initialize(options = nil)
  super(options)

  options = { :logger => self.class.logger }.merge(options || {})
  if defined?(Hiredis)
    options = { :driver => :hiredis }.merge(options || {})
  end
  @redis = ::Redis.new(options)

  if (@redis.info["redis_version"].split(".").map { |x| x.to_i } <=> [2, 1, 3]) < 0
    $stderr.puts "Redis server with version < 2.1.3 has bug with increment/decrement on values with expiring time! Please upgrade Redis server or don't use increment/decrement methods with :expires_in option."
  end

  extend Strategy::LocalCache
end

Instance Method Details

#clear(options = nil) ⇒ Object



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

def clear(options = nil)
  @redis.flushdb
end

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



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

def decrement(name, amount = 1, options = nil)
  options = merged_options(options)
  expires_in = options[:expires_in].to_i
  redis_key = namespaced_key(name, options)
  response = instrument(:decrement, name, :amount => amount) do
    r = @redis.decrby(redis_key, amount)
    if expires_in > 0
      @redis.expire(redis_key, expires_in)
    end
    r
  end
  response
end

#disconnectObject



33
34
35
# File 'lib/active_support/cache/redis_store.rb', line 33

def disconnect
  @redis and @redis.client.disconnect
end

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



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_support/cache/redis_store.rb', line 41

def increment(name, amount = 1, options = nil)
  options = merged_options(options)
  expires_in = options[:expires_in].to_i
  redis_key = namespaced_key(name, options)
  response = instrument(:increment, name, :amount => amount) do
    r = @redis.incrby(redis_key, amount)
    if expires_in > 0
      @redis.expire(redis_key, expires_in)
    end
    r
  end
  response
end

#read_multi(*names) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_support/cache/redis_store.rb', line 69

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 = @redis.mget(*keys_to_names.keys)
  values = {}
  keys_to_names.keys.zip(raw_values).each do |key, value|
    entry = deserialize_entry(value)
    values[keys_to_names[key]] = entry.value unless entry.nil? || entry.expired?
  end
  values
end

#reconnectObject



29
30
31
# File 'lib/active_support/cache/redis_store.rb', line 29

def reconnect
  @redis and @redis.client.reconnect
end