Class: Sinatra::Cache::RedisStore

Inherits:
Object
  • Object
show all
Defined in:
lib/cache/sinatra/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


30
31
32
# File 'lib/cache/sinatra/redis_store.rb', line 30

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

Instance Method Details

#clearObject

Clear all the data from the store.



117
118
119
# File 'lib/cache/sinatra/redis_store.rb', line 117

def clear
  @data.flushdb
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"


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

def decrement(key, amount = 1)
  @data.decrby key, amount
end

#delete(key, options = nil) ⇒ Object



46
47
48
# File 'lib/cache/sinatra/redis_store.rb', line 46

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

#delete_matched(matcher, options = nil) ⇒ Object

Delete objects for matched keys.

Example:

cache.del_matched "rab*"


108
109
110
# File 'lib/cache/sinatra/redis_store.rb', line 108

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

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/cache/sinatra/redis_store.rb', line 50

def exist?(key, options = nil)
  @data.exists key
end

#fetch(key, options = {}) ⇒ Object



112
113
114
# File 'lib/cache/sinatra/redis_store.rb', line 112

def fetch(key, options = {})
  (!options[:force] && data = read(key, options)) || (write key, yield, options if block_given?)
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"


75
76
77
# File 'lib/cache/sinatra/redis_store.rb', line 75

def increment(key, amount = 1)
  @data.incrby key, amount
end

#read(key, options = nil) ⇒ Object



42
43
44
# File 'lib/cache/sinatra/redis_store.rb', line 42

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

#statsObject



121
122
123
# File 'lib/cache/sinatra/redis_store.rb', line 121

def stats
  @data.info
end

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



34
35
36
37
38
39
40
# File 'lib/cache/sinatra/redis_store.rb', line 34

def write(key, value, options = nil)
  if options && options[:unless_exist]
    @data.setnx key, value, options
  else
    @data.set key, value, options
  end
end