Class: ActiveSupport::Cache::RedisStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::RedisStore
- Defined in:
- lib/redis_store/cache/redis_store.rb
Overview
A cache store implementation which stores data in Redis.
Instance Method Summary collapse
- #clear ⇒ Object
-
#decrement(key, amount = 1) ⇒ Object
Note: if key does not exist it will be initialized and return ‘0’.
-
#delete(key, options = nil) ⇒ Object
Deleted a named value from the cache.
-
#delete_matched(matcher, options = nil) ⇒ Object
Note: to comply with the established cache store interface, matcher should be a Regexp.
-
#exist?(key, options = nil) ⇒ Boolean
Check for the existence of a particular key value.
-
#increment(key, amount = 1) ⇒ Object
Note: if key does not exist it will be initialized and return ‘1’.
-
#initialize(address_or_options = {}) ⇒ RedisStore
constructor
Creates a new RedisStore client object, with the given Redis server address.
-
#read(key, options = nil) ⇒ Object
Read a value from the cache.
- #stats ⇒ Object
-
#write(key, value, options = nil) ⇒ Object
Write a value to the cache.
Constructor Details
#initialize(address_or_options = {}) ⇒ RedisStore
Creates a new RedisStore client object, with the given Redis server address. The address should be given as a string. For example:
ActiveSupport::Cache::RedisStore.new("localhost:6379/22")
If no addresses are specified, then RedisStore will connect to the default Redis server port on localhost.
13 14 15 |
# File 'lib/redis_store/cache/redis_store.rb', line 13 def initialize( = {}) @data = ::RedisStore::MarshalledClient.new(::RedisStore::Config.new()) end |
Instance Method Details
#clear ⇒ Object
72 73 74 |
# File 'lib/redis_store/cache/redis_store.rb', line 72 def clear @data.flushdb end |
#decrement(key, amount = 1) ⇒ Object
Note: if key does not exist it will be initialized and return ‘0’.
68 69 70 |
# File 'lib/redis_store/cache/redis_store.rb', line 68 def decrement(key, amount = 1) @data.decrby(key, amount) end |
#delete(key, options = nil) ⇒ Object
Deleted a named value from the cache.
38 39 40 41 |
# File 'lib/redis_store/cache/redis_store.rb', line 38 def delete(key, = nil) super @data.del(key) end |
#delete_matched(matcher, options = nil) ⇒ Object
Note: to comply with the established cache store interface, matcher should be a Regexp. However, converting Regexps to globs (supported by Redis) is non-trivial.
For now, we accept a string. Sorry about the conditional logic that is sure to follow :(.
56 57 58 59 60 |
# File 'lib/redis_store/cache/redis_store.rb', line 56 def delete_matched(matcher, = nil) super raise ArgumentError.new("Sorry, matcher must be a String") unless matcher.is_a?(String) @data.keys(matcher).each { |key| @data.del(key) } end |
#exist?(key, options = nil) ⇒ Boolean
Check for the existence of a particular key value.
44 45 46 47 |
# File 'lib/redis_store/cache/redis_store.rb', line 44 def exist?(key, = nil) super @data.exists(key) end |
#increment(key, amount = 1) ⇒ Object
Note: if key does not exist it will be initialized and return ‘1’.
63 64 65 |
# File 'lib/redis_store/cache/redis_store.rb', line 63 def increment(key, amount = 1) @data.incrby(key, amount) end |
#read(key, options = nil) ⇒ Object
Read a value from the cache.
18 19 20 21 |
# File 'lib/redis_store/cache/redis_store.rb', line 18 def read(key, = nil) super @data.marshalled_get(key, ) end |
#stats ⇒ Object
76 77 78 |
# File 'lib/redis_store/cache/redis_store.rb', line 76 def stats @data.info end |
#write(key, value, options = nil) ⇒ Object
Write a value to the cache.
Possible options:
-
:unless_exist
- set to true if you don’t want to update the cache if the key is already set. -
:expires_in
- the number of seconds that this value may stay in the cache. See ActiveSupport::Cache::Store#write for an example.
31 32 33 34 35 |
# File 'lib/redis_store/cache/redis_store.rb', line 31 def write(key, value, = nil) super method = && [:unless_exist] ? :marshalled_setnx : :marshalled_set @data.send(method, key, value, ) end |