Class: ActiveSupport::Cache::RedisStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::RedisStore
- Defined in:
- lib/active_support/cache/redis_store.rb
Instance Method Summary collapse
- #clear(options = nil) ⇒ Object
- #decrement(name, amount = 1, options = nil) ⇒ Object
- #disconnect ⇒ Object
- #increment(name, amount = 1, options = nil) ⇒ Object
-
#initialize(options = nil) ⇒ RedisStore
constructor
A new instance of RedisStore.
- #read_multi(*names) ⇒ Object
- #reconnect ⇒ Object
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( = nil) super() = { :logger => self.class.logger }.merge( || {}) if defined?(Hiredis) = { :driver => :hiredis }.merge( || {}) end @redis = ::Redis.new() 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( = 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, = nil) = () expires_in = [:expires_in].to_i redis_key = namespaced_key(name, ) 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 |
#disconnect ⇒ Object
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, = nil) = () expires_in = [:expires_in].to_i redis_key = namespaced_key(name, ) 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) = names. = () keys_to_names = Hash[names.map{|name| [namespaced_key(name, ), 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 |
#reconnect ⇒ Object
29 30 31 |
# File 'lib/active_support/cache/redis_store.rb', line 29 def reconnect @redis and @redis.client.reconnect end |