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(*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 "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


26
27
28
29
# File 'lib/active_support/cache/redis_store.rb', line 26

def initialize(*addresses)
  @data = ::Redis::Factory.create(addresses)
  super(addresses.extract_options!)
end

Instance Method Details

#clearObject

Clear all the data from the store.



127
128
129
130
131
# File 'lib/active_support/cache/redis_store.rb', line 127

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"


120
121
122
123
124
# File 'lib/active_support/cache/redis_store.rb', line 120

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.

Example:

cache.del_matched "rab*"


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

def delete_matched(matcher, options = nil)
  options = merged_options(options)
  instrument(:delete_matched, matcher.inspect) do
    matcher = key_matcher(matcher, options)
    begin
      !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
    rescue Errno::ECONNREFUSED => e
      false
    end
  end
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"


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

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


61
62
63
64
65
66
67
68
69
70
# File 'lib/active_support/cache/redis_store.rb', line 61

def read_multi(*names)
  values = @data.mget(*names)

  # Remove the options hash before mapping keys to values
  names.extract_options!

  result = Hash[names.zip(values)]
  result.reject!{ |k,v| v.nil? }
  result
end

#reconnectObject

Force client reconnection, useful Unicorn deployed apps.



138
139
140
# File 'lib/active_support/cache/redis_store.rb', line 138

def reconnect
  @data.reconnect
end

#statsObject



133
134
135
# File 'lib/active_support/cache/redis_store.rb', line 133

def stats
  @data.info
end

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



31
32
33
34
35
36
37
# File 'lib/active_support/cache/redis_store.rb', line 31

def write(name, value, options = nil)
  options = merged_options(options)
  instrument(:write, name, options) do |payload|
    entry = options[:raw].present? ? value : Entry.new(value, options)
    write_entry(namespaced_key(name, options), entry, options)
  end
end