Class: Sinatra::Cache::RedisStore
- Inherits:
-
Object
- Object
- Sinatra::Cache::RedisStore
- Defined in:
- lib/sinatra/cache/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(key, options = nil) ⇒ Object
-
#delete_matched(matcher, options = nil) ⇒ Object
Delete objects for matched keys.
- #exist?(key, options = nil) ⇒ Boolean
- #fetch(key, options = {}) ⇒ Object
-
#increment(key, amount = 1) ⇒ Object
Increment a key in the store.
-
#initialize(*addresses) ⇒ RedisStore
constructor
Instantiate the store.
- #read(key, options = nil) ⇒ Object
- #stats ⇒ Object
- #write(key, value, options = nil) ⇒ Object
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
31 32 33 |
# File 'lib/sinatra/cache/redis_store.rb', line 31 def initialize(*addresses) @data = Redis::Store::Factory.create addresses end |
Instance Method Details
#clear ⇒ Object
Clear all the data from the store.
122 123 124 |
# File 'lib/sinatra/cache/redis_store.rb', line 122 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"
101 102 103 |
# File 'lib/sinatra/cache/redis_store.rb', line 101 def decrement(key, amount = 1) @data.decrby key, amount end |
#delete(key, options = nil) ⇒ Object
47 48 49 |
# File 'lib/sinatra/cache/redis_store.rb', line 47 def delete(key, = nil) @data.del key end |
#delete_matched(matcher, options = nil) ⇒ Object
Delete objects for matched keys.
Example:
cache.del_matched "rab*"
109 110 111 |
# File 'lib/sinatra/cache/redis_store.rb', line 109 def delete_matched(matcher, = nil) @data.keys(matcher).each { |key| @data.del key } end |
#exist?(key, options = nil) ⇒ Boolean
51 52 53 |
# File 'lib/sinatra/cache/redis_store.rb', line 51 def exist?(key, = nil) @data.exists key end |
#fetch(key, options = {}) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/sinatra/cache/redis_store.rb', line 113 def fetch(key, = {}) (![:force] && data = read(key, )) || block_given? && begin data = yield write(key, data, ) end data || nil 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"
76 77 78 |
# File 'lib/sinatra/cache/redis_store.rb', line 76 def increment(key, amount = 1) @data.incrby key, amount end |
#read(key, options = nil) ⇒ Object
43 44 45 |
# File 'lib/sinatra/cache/redis_store.rb', line 43 def read(key, = nil) @data.get(key, ) end |
#stats ⇒ Object
126 127 128 |
# File 'lib/sinatra/cache/redis_store.rb', line 126 def stats @data.info end |
#write(key, value, options = nil) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/sinatra/cache/redis_store.rb', line 35 def write(key, value, = nil) if && [:unless_exist] @data.setnx key, value, else @data.set key, value, end end |