Class: Moneta::Redis
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(options = {}) ⇒ Redis
constructor
A new instance of Redis.
- #key?(key) ⇒ Boolean (also: #has_key?)
- #store(key, value, options = {}) ⇒ Object
- #update_key(key, options = {}) ⇒ Object
Methods included from Defaults
Constructor Details
#initialize(options = {}) ⇒ Redis
Returns a new instance of Redis.
12 13 14 |
# File 'lib/moneta/redis.rb', line 12 def initialize( = {}) @cache = ::Redis.new() end |
Instance Method Details
#[](key) ⇒ Object
22 23 24 |
# File 'lib/moneta/redis.rb', line 22 def [](key) @cache.get(key) end |
#[]=(key, value) ⇒ Object
26 27 28 |
# File 'lib/moneta/redis.rb', line 26 def []=(key, value) store(key, value) end |
#clear ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/moneta/redis.rb', line 59 def clear if @cache.respond_to?(:flush_db) @cache.flush_db else @cache.flushdb end end |
#delete(key) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/moneta/redis.rb', line 30 def delete(key) value = @cache[key] if @cache.respond_to?(:delete) @cache.delete(key) if value else @cache.del(key) if value end value end |
#key?(key) ⇒ Boolean Also known as: has_key?
16 17 18 |
# File 'lib/moneta/redis.rb', line 16 def key?(key) !@cache[key].nil? end |
#store(key, value, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/moneta/redis.rb', line 40 def store(key, value, = {}) if .has_key?(:expires_in) if @cache.respond_to?(:setex) @cache.setex(key, [:expires_in], value) elsif @cache.respond_to?(:set_with_expire) @cache.set_with_expire(key, value, [:expires_in]) else @cache.set(key, value, [:expires_in]) end else @cache.set(key, value) end end |
#update_key(key, options = {}) ⇒ Object
54 55 56 57 |
# File 'lib/moneta/redis.rb', line 54 def update_key(key, = {}) val = @cache[key] self.store(key, val, ) end |