Class: Moneta::Adapters::ActiveSupportCache
- Inherits:
-
Moneta::Adapter
- Object
- Moneta::Adapter
- Moneta::Adapters::ActiveSupportCache
- Includes:
- Rails5Support, ExpiresSupport
- Defined in:
- lib/moneta/adapters/activesupportcache.rb
Overview
ActiveSupport::Cache::Store adapter
Defined Under Namespace
Modules: Rails5Support
Instance Attribute Summary
Attributes inherited from Moneta::Adapter
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this 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 = {}) ⇒ Object constructor
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#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 inherited from Moneta::Adapter
backend, backend_block, backend_required?
Methods included from Config
Methods included from Defaults
#[], #[]=, #close, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Object
14 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 14 backend { Rails.cache if defined?(Rails) } |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
76 77 78 79 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 76 def clear( = {}) backend.clear self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
67 68 69 70 71 72 73 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 67 def delete(key, = {}) value = backend_read(key, ) if value != nil backend_delete(key, **) [:raw] ? value.to_s : value end 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.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 53 def increment(key, amount = 1, = {}) expires = expires_value() .delete(:raw) existing = Integer(backend_fetch(key, raw: true, **) { 0 }) if amount > 0 backend_increment(key, amount, expires_in: expires ? expires.seconds : nil, **) elsif amount < 0 backend_decrement(key, -amount, expires_in: expires ? expires.seconds : nil, **) else existing end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 17 def key?(key, = {}) exists = begin backend_exist?(key) rescue ArgumentError, TypeError # these errors happen when certain adapters try to deserialize # values, which means there's something present true end if exists && (expires = expires_value(, nil)) != nil value = backend_read(key, **) backend_write(key, value, expires_in: expires ? expires.seconds : nil, **) end exists end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
36 37 38 39 40 41 42 43 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 36 def load(key, = {}) expires = expires_value(, nil) value = backend_read(key, **) if value and expires != nil backend_write(key, value, expires_in: expires ? expires.seconds : nil, **) end value end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 104 def merge!(pairs, = {}) if block_given? existing = slice(*pairs.map { |k, _| k }, **) pairs = pairs.map do |key, new_value| if existing.key?(key) new_value = yield(key, existing[key], new_value) end [key, new_value] end end hash = Hash === pairs ? pairs : Hash[pairs.to_a] expires = expires_value() backend_write_multi(hash, expires_in: expires ? expires.seconds : nil, **) 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.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 82 def slice(*keys, **) hash = backend.read_multi(*keys) if (expires = expires_value(, nil)) != nil hash.each do |key, value| backend_write(key, value, expires_in: expires ? expires.seconds : nil, **) end end if [:raw] hash.each do |key, value| hash[key] = value.to_s if value != nil end end hash end |
#store(key, value, options = {}) ⇒ Object
Store value with key
46 47 48 49 50 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 46 def store(key, value, = {}) expires = expires_value() backend_write(key, value, expires_in: expires ? expires.seconds : nil, **) 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.
98 99 100 101 |
# File 'lib/moneta/adapters/activesupportcache.rb', line 98 def values_at(*keys, **) hash = slice(*keys, **) keys.map { |key| hash[key] } end |