Class: Moneta::Adapters::MemcachedDalli
- Inherits:
-
Object
- Object
- Moneta::Adapters::MemcachedDalli
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/memcached/dalli.rb
Overview
Memcached backend (using gem dalli)
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ MemcachedDalli
constructor
A new instance of MemcachedDalli.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #decrement, #fetch, #key?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ MemcachedDalli
Returns a new instance of MemcachedDalli.
14 15 16 17 18 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 14 def initialize( = {}) [:expires_in] = .delete(:expires) server = .delete(:server) || '127.0.0.1:11211' @cache = ::Dalli::Client.new(server, ) end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
61 62 63 64 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 61 def clear( = {}) @cache.flush_all self end |
#close ⇒ Object
Explicitly close the store
67 68 69 70 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 67 def close @cache.close nil end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
37 38 39 40 41 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 37 def delete(key, = {}) value = @cache.get(key) @cache.delete(key) value end |
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key
Not every Moneta store implements this method, a NotImplementedError if it is not supported.
This method also accepts negative amounts.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 44 def increment(key, amount = 1, = {}) # FIXME: There is a Dalli bug, load(key) returns a wrong value after increment # therefore we set default = nil and create the counter manually result = if amount >= 0 @cache.incr(key, amount, [:expires], nil) else @cache.decr(key, -amount, [:expires], nil) end if result result else store(key, amount, ) amount end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
21 22 23 24 25 26 27 28 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 21 def load(key, = {}) value = @cache.get(key) if value && .include?(:expires) store(key, value, ) else value end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
31 32 33 34 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 31 def store(key, value, = {}) @cache.set(key, value, [:expires], :raw => true) value end |