Class: ActiveSupport::Cache::FaultTolerantRedisStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::FaultTolerantRedisStore
- Defined in:
- lib/active_support/cache/fault_tolerant_redis_store.rb
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all the data from the store.
-
#decrement(key, amount = 1) ⇒ Object
Decrement a key in the store.
-
#delete_matched(matcher, options = nil) ⇒ Object
Delete objects for matched keys.
-
#exist?(name, options = nil) ⇒ Boolean
fixed problem with invalid exists? method github.com/rails/rails/commit/cad2c8f5791d5bd4af0f240d96e00bae76eabd2f.
- #expire(key, ttl) ⇒ Object
-
#increment(key, amount = 1) ⇒ Object
Increment a key in the store.
-
#initialize(*addresses) ⇒ FaultTolerantRedisStore
constructor
Instantiate the store.
-
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys.
-
#reconnect ⇒ Object
Force client reconnection, useful Unicorn deployed apps.
- #stats ⇒ Object
- #write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize(*addresses) ⇒ FaultTolerantRedisStore
Instantiate the store.
Example:
RedisStore.new
# => host: localhost, port: 6379, db: 0
RedisStore.new "example.com"
# => host: example.com, port: 6379, db: 0
RedisStore.new "example.com:23682"
# => host: example.com, port: 23682, db: 0
RedisStore.new "example.com:23682/1"
# => host: example.com, port: 23682, db: 1
RedisStore.new "example.com:23682/1/theplaylist"
# => host: example.com, port: 23682, db: 1, namespace: theplaylist
RedisStore.new "localhost:6379/0", "localhost:6380/0"
# => instantiate a cluster
28 29 30 31 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 28 def initialize(*addresses) @data = MaybeClient.new(client: ::Redis::Store::Factory.create(addresses)) super(addresses.) end |
Instance Method Details
#clear ⇒ Object
Clear all the data from the store.
139 140 141 142 143 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 139 def clear instrument(:clear, nil, nil) do @data.flushdb end end |
#decrement(key, amount = 1) ⇒ Object
Decrement a key in the store
If the key doesn’t exist it will be initialized on 0. If the key exist but it isn’t a Fixnum it will be initialized on 0.
Example:
We have two objects in cache:
counter # => 23
rabbit # => #<Rabbit:0x5eee6c>
cache.decrement "counter"
cache.read "counter", :raw => true # => "22"
cache.decrement "counter", 2
cache.read "counter", :raw => true # => "20"
cache.decrement "a counter"
cache.read "a counter", :raw => true # => "-1"
cache.decrement "rabbit"
cache.read "rabbit", :raw => true # => "-1"
128 129 130 131 132 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 128 def decrement(key, amount = 1) instrument(:decrement, key, :amount => amount) do @data.decrby key, amount end end |
#delete_matched(matcher, options = nil) ⇒ Object
Delete objects for matched keys.
Performance note: this operation can be dangerous for large production databases, as it uses the Redis “KEYS” command, which is O(N) over the total number of keys in the database. Users of large Redis caches should avoid this method.
Example:
cache.del_matched "rab*"
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 50 def delete_matched(matcher, = nil) = () instrument(:delete_matched, matcher.inspect) do matcher = key_matcher(matcher, ) begin !(keys = @data.keys(matcher)).empty? && @data.del(*keys) rescue Errno::ECONNREFUSED => e false end end end |
#exist?(name, options = nil) ⇒ Boolean
fixed problem with invalid exists? method github.com/rails/rails/commit/cad2c8f5791d5bd4af0f240d96e00bae76eabd2f
147 148 149 150 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 147 def exist?(name, = nil) res = super(name, ) res || false end |
#expire(key, ttl) ⇒ Object
134 135 136 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 134 def expire(key, ttl) @data.expire key, ttl end |
#increment(key, amount = 1) ⇒ Object
Increment a key in the store.
If the key doesn’t exist it will be initialized on 0. If the key exist but it isn’t a Fixnum it will be initialized on 0.
Example:
We have two objects in cache:
counter # => 23
rabbit # => #<Rabbit:0x5eee6c>
cache.increment "counter"
cache.read "counter", :raw => true # => "24"
cache.increment "counter", 6
cache.read "counter", :raw => true # => "30"
cache.increment "a counter"
cache.read "a counter", :raw => true # => "1"
cache.increment "rabbit"
cache.read "rabbit", :raw => true # => "1"
101 102 103 104 105 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 101 def increment(key, amount = 1) instrument(:increment, key, :amount => amount) do @data.incrby key, amount end end |
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys. Options can be passed in the last argument.
Example:
cache.read_multi "rabbit", "white-rabbit"
cache.read_multi "rabbit", "white-rabbit", :raw => true
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 68 def read_multi(*names) values = @data.mget(*names) values.map! { |v| v.is_a?(ActiveSupport::Cache::Entry) ? v.value : v } # Remove the options hash before mapping keys to values names. result = Hash[names.zip(values)] result.reject!{ |k,v| v.nil? } result end |
#reconnect ⇒ Object
Force client reconnection, useful Unicorn deployed apps.
157 158 159 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 157 def reconnect @data.reconnect end |
#stats ⇒ Object
152 153 154 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 152 def stats @data.info end |
#write(name, value, options = nil) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/active_support/cache/fault_tolerant_redis_store.rb', line 33 def write(name, value, = nil) = () instrument(:write, name, ) do |payload| entry = [:raw].present? ? value : Entry.new(value, ) write_entry(namespaced_key(name, ), entry, ) end end |