Sha256: a5df6178582050c4f2588809fcf5a315b2497482ecf7131a45cd1cea593ddcf8
Contents?: true
Size: 1.49 KB
Versions: 13
Compression:
Stored size: 1.49 KB
Contents
module Lawnchair module StorageEngine class Composite attr_reader :storage_engines def initialize(*args) @storage_engines = [] args.each do |arg| register_storage_engine arg end end def register_storage_engine(storage_engine) klass = storage_engine == :redis ? "Redis" : "InProcess" storage_engines << Object.module_eval("Lawnchair::StorageEngine::#{klass}") end def fetch(key, options, &block) raise "No Storage Engines Configured" if storage_engines.empty? value, index = find_in_storage(key, options) value ||= yield place_in_storage(key, value, options, index) end private def find_in_storage(key, options) value, index = nil, nil storage_engines.each_with_index do |storage_engine, i| if storage_engine.db_connection? if storage_engine.exists?(key) value = storage_engine.get(key, options) index = i break end else value = nil index = i end end return value, index end def place_in_storage(key, value, options, index) storage_engines.each_with_index do |storage_engine, i| break if i == index if storage_engine.db_connection? storage_engine.set(key, value, options) end end value end end end end
Version data entries
13 entries across 13 versions & 1 rubygems