Class: ActiveSupport::Cache::RedisStore

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

Instance Method Summary collapse

Constructor Details

#initialize(*addresses) ⇒ RedisStore

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 "localhost:6379/0", "localhost:6380/0" # => instantiate a cluster


12
13
14
# File 'lib/cache/rails/redis_store.rb', line 12

def initialize(*addresses)
  @data = RedisFactory.create(addresses)
end

Instance Method Details

#clearObject

Clear all the data from the store.



99
100
101
102
# File 'lib/cache/rails/redis_store.rb', line 99

def clear
  log "clear", nil, nil
  @data.flush_db
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"


84
85
86
87
# File 'lib/cache/rails/redis_store.rb', line 84

def decrement(key, amount = 1)
  log "decrement", key, amount
  @data.decr key, amount
end

#delete(key, options = nil) ⇒ Object



27
28
29
30
# File 'lib/cache/rails/redis_store.rb', line 27

def delete(key, options = nil)
  super
  @data.delete key
end

#delete_matched(matcher, options = nil) ⇒ Object

Delete objects for matched keys.

Example:

cache.delete_matched "rab*"


93
94
95
96
# File 'lib/cache/rails/redis_store.rb', line 93

def delete_matched(matcher, options = nil)
  super
  @data.keys(matcher).each { |key| @data.delete key }
end

#exist?(key, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def exist?(key, options = nil)
  super
  @data.key? key
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"


58
59
60
61
# File 'lib/cache/rails/redis_store.rb', line 58

def increment(key, amount = 1)
  log "increment", key, amount
  @data.incr key, amount
end

#read(key, options = nil) ⇒ Object



22
23
24
25
# File 'lib/cache/rails/redis_store.rb', line 22

def read(key, options = nil)
  super
  @data.get key, options
end

#statsObject



104
105
106
# File 'lib/cache/rails/redis_store.rb', line 104

def stats
  @data.info
end

#write(key, value, options = nil) ⇒ Object



16
17
18
19
20
# File 'lib/cache/rails/redis_store.rb', line 16

def write(key, value, options = nil)
  super
  method = options && options[:unless_exist] ? :set_unless_exists : :set
  @data.send method, key, value, options
end