Class: Moneta::Adapters::MemcachedDalli
- Inherits:
-
Object
- Object
- Moneta::Adapters::MemcachedDalli
- Includes:
- Defaults, ExpiresSupport
- Defined in:
- lib/moneta/adapters/memcached/dalli.rb
Overview
Memcached backend (using gem dalli)
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Attributes included from ExpiresSupport
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#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.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#slice(*keys, **options) ⇒ <(Object, Object)>
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods included from ExpiresSupport
Methods included from Defaults
#[], #[]=, #decrement, #each_key, #features, #fetch, #fetch_values, included, #key?, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ MemcachedDalli
Returns a new instance of MemcachedDalli.
19 20 21 22 23 24 25 26 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 19 def initialize( = {}) self.default_expires = .delete(:expires) @backend = [:backend] || begin server = .delete(:server) || '127.0.0.1:11211' ::Dalli::Client.new(server, ) end end |
Instance Attribute Details
#backend ⇒ Object (readonly)
12 13 14 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 12 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
69 70 71 72 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 69 def clear( = {}) @backend.flush_all self end |
#close ⇒ Object
Explicitly close the store
80 81 82 83 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 80 def close @backend.close nil end |
#create(key, value, options = {}) ⇒ Boolean
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically sets a key to value if it’s not set.
75 76 77 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 75 def create(key, value, = {}) !!@backend.add(key, value, expires_value() || nil, raw: true) end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
45 46 47 48 49 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 45 def delete(key, = {}) value = @backend.get(key) @backend.delete(key) value end |
#increment(key, amount = 1, options = {}) ⇒ Object
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically increment integer value with key
This method also accepts negative amounts.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 52 def increment(key, amount = 1, = {}) result = if amount >= 0 @backend.incr(key, amount, expires_value() || nil) else @backend.decr(key, -amount, expires_value() || nil) end if result result elsif create(key, amount.to_s, ) amount else increment(key, amount, ) end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
29 30 31 32 33 34 35 36 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 29 def load(key, = {}) value = @backend.get(key) if value expires = expires_value(, nil) @backend.set(key, value, expires || nil, raw: true) if expires != nil value end end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses Defaults#key?, #load and #store.
Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 108 def merge!(pairs, = {}) expires = expires_value() expires = expires.to_i if Numeric === expires expires ||= nil if block_given? keys = pairs.map { |key, _| key }.to_a old_pairs = @backend.get_multi(keys) pairs = pairs.map do |key, new_value| if old_pairs.key? key new_value = yield(key, old_pairs[key], new_value) end [key, new_value] end end @backend.multi do pairs.each do |key, value| @backend.set(key, value, expires, raw: true) end end self end |
#slice(*keys, **options) ⇒ <(Object, Object)>
The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).
Some adapters may implement this method atomically. The default implmentation uses #values_at.
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 86 def slice(*keys, **) @backend.get_multi(keys).tap do |pairs| next if pairs.empty? expires = expires_value(, nil) next if expires == nil expires = expires.to_i if Numeric === expires expires ||= 0 @backend.multi do pairs.each do |key, value| @backend.set(key, value, expires, false) end end end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
39 40 41 42 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 39 def store(key, value, = {}) @backend.set(key, value, expires_value() || nil, raw: true) value end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.
Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.
102 103 104 105 |
# File 'lib/moneta/adapters/memcached/dalli.rb', line 102 def values_at(*keys, **) pairs = slice(*keys, **) keys.map { |key| pairs.delete(key) } end |