class Moneta::Adapters::ActiveSupportCache
ActiveSupport::Cache::Store adapter @api public
Public Instance Methods
clear(options = {})
click to toggle source
(see Proxy#clear
)
# File lib/moneta/adapters/activesupportcache.rb, line 76 def clear(options = {}) backend.clear self end
delete(key, options = {})
click to toggle source
(see Proxy#delete
)
# File lib/moneta/adapters/activesupportcache.rb, line 67 def delete(key, options = {}) value = backend_read(key, options) if value != nil backend_delete(key, **options) options[:raw] ? value.to_s : value end end
increment(key, amount = 1, options = {})
click to toggle source
(see Proxy#increment
)
# File lib/moneta/adapters/activesupportcache.rb, line 53 def increment(key, amount = 1, options = {}) expires = expires_value(options) options.delete(:raw) existing = Integer(backend_fetch(key, raw: true, **options) { 0 }) if amount > 0 backend_increment(key, amount, expires_in: expires ? expires.seconds : nil, **options) elsif amount < 0 backend_decrement(key, -amount, expires_in: expires ? expires.seconds : nil, **options) else existing end end
key?(key, options = {})
click to toggle source
(see Proxy#key?
)
# File lib/moneta/adapters/activesupportcache.rb, line 17 def key?(key, options = {}) 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(options, nil)) != nil value = backend_read(key, **options) backend_write(key, value, expires_in: expires ? expires.seconds : nil, **options) end exists end
load(key, options = {})
click to toggle source
(see Proxy#load
)
# File lib/moneta/adapters/activesupportcache.rb, line 36 def load(key, options = {}) expires = expires_value(options, nil) value = backend_read(key, **options) if value and expires != nil backend_write(key, value, expires_in: expires ? expires.seconds : nil, **options) end value end
merge!(pairs, options = {}) { |key, existing, new_value| ... }
click to toggle source
(see Proxy#merge!
)
# File lib/moneta/adapters/activesupportcache.rb, line 104 def merge!(pairs, options = {}) if block_given? existing = slice(*pairs.map { |k, _| k }, **options) 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(options) backend_write_multi(hash, expires_in: expires ? expires.seconds : nil, **options) self end
slice(*keys, **options)
click to toggle source
(see Proxy#slice
)
# File lib/moneta/adapters/activesupportcache.rb, line 82 def slice(*keys, **options) hash = backend.read_multi(*keys) if (expires = expires_value(options, nil)) != nil hash.each do |key, value| backend_write(key, value, expires_in: expires ? expires.seconds : nil, **options) end end if options[:raw] hash.each do |key, value| hash[key] = value.to_s if value != nil end end hash end
store(key, value, options = {})
click to toggle source
(see Proxy#store
)
# File lib/moneta/adapters/activesupportcache.rb, line 46 def store(key, value, options = {}) expires = expires_value(options) backend_write(key, value, expires_in: expires ? expires.seconds : nil, **options) value end
values_at(*keys, **options)
click to toggle source
(see Proxy#values_at
)
# File lib/moneta/adapters/activesupportcache.rb, line 98 def values_at(*keys, **options) hash = slice(*keys, **options) keys.map { |key| hash[key] } end
Private Instance Methods
expires_value(options, default = config.expires)
click to toggle source
Calls superclass method
Moneta::ExpiresSupport#expires_value
# File lib/moneta/adapters/activesupportcache.rb, line 124 def expires_value(options, default = config.expires) super.tap { options.delete(:expires) unless options.frozen? } end