class Moneta::Cache
Combines two stores. One is used as cache, the other as backend adapter.
@example Add ‘Moneta::Cache` to proxy stack
Moneta.build do use(:Cache) do adapter { adapter :File, dir: 'data' } cache { adapter :Memory } end end
@api public
Attributes
adapter[RW]
cache[RW]
Public Class Methods
new(options = {}, &block)
click to toggle source
@param [Hash] options Options hash @option options [Moneta store] :cache Moneta
store used as cache @option options [Moneta store] :adapter Moneta
store used as adapter @yieldparam Builder
block
# File lib/moneta/cache.rb, line 44 def initialize(options = {}, &block) @cache, @adapter = options[:cache], options[:adapter] DSL.new(self, &block) if block_given? end
Public Instance Methods
clear(options = {})
click to toggle source
(see Proxy#clear
)
# File lib/moneta/cache.rb, line 92 def clear(options = {}) @cache.clear(options) @adapter.clear(options) self end
close()
click to toggle source
(see Proxy#close
)
# File lib/moneta/cache.rb, line 99 def close @cache.close @adapter.close end
create(key, value, options = {})
click to toggle source
(see Proxy#create
)
# File lib/moneta/cache.rb, line 76 def create(key, value, options = {}) if @adapter.create(key, value, options) @cache.store(key, value, options) true else false end end
delete(key, options = {})
click to toggle source
(see Proxy#delete
)
# File lib/moneta/cache.rb, line 86 def delete(key, options = {}) @cache.delete(key, options) @adapter.delete(key, options) end
each_key(&block)
click to toggle source
(see Proxy#each_key
)
# File lib/moneta/cache.rb, line 105 def each_key(&block) raise NotImplementedError, 'adapter doesn\'t support #each_key' \ unless supports? :each_key return enum_for(:each_key) unless block_given? @adapter.each_key(&block) self end
features()
click to toggle source
(see Proxy#features
)
# File lib/moneta/cache.rb, line 115 def features @features ||= ((@cache.features + [:create, :increment, :each_key]) & @adapter.features).freeze end
increment(key, amount = 1, options = {})
click to toggle source
(see Proxy#increment
)
# File lib/moneta/cache.rb, line 70 def increment(key, amount = 1, options = {}) @cache.delete(key, options) @adapter.increment(key, amount, options) end
key?(key, options = {})
click to toggle source
(see Proxy#key?
)
# File lib/moneta/cache.rb, line 50 def key?(key, options = {}) @cache.key?(key, options) || @adapter.key?(key, options) end
load(key, options = {})
click to toggle source
(see Proxy#load
)
# File lib/moneta/cache.rb, line 55 def load(key, options = {}) if options[:sync] || (value = @cache.load(key, options)) == nil value = @adapter.load(key, options) @cache.store(key, value, options) if value != nil end value end
store(key, value, options = {})
click to toggle source
(see Proxy#store
)
# File lib/moneta/cache.rb, line 64 def store(key, value, options = {}) @cache.store(key, value, options) @adapter.store(key, value, options) end