Sha256: 6bfbf2e3b60cf1803dd88d72d275a4c6561ccd889be1b89c4ce1f33f6da9ff63

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module IdentityCache
  class FallbackFetcher
    attr_accessor :cache_backend

    def initialize(cache_backend)
      @cache_backend = cache_backend
    end

    def write(key, value)
      @cache_backend.write(key, value) if IdentityCache.should_update_cache?
    end

    def delete(key)
      @cache_backend.delete(key) if IdentityCache.should_update_cache?
    end

    def clear
      @cache_backend.clear if IdentityCache.should_update_cache?
    end

    def fetch_multi(keys, &block)
      results = @cache_backend.read_multi(*keys)
      missed_keys = keys - results.keys
      unless missed_keys.empty?
        replacement_results = yield missed_keys
        missed_keys.zip(replacement_results) do |key, replacement_result|
          @cache_backend.write(key, replacement_result) if IdentityCache.should_update_cache?
          results[key] = replacement_result
        end
      end
      results
    end

    def fetch(key)
      result = @cache_backend.read(key)
      if result.nil?
        result = yield
        @cache_backend.write(key, result) if IdentityCache.should_update_cache?
      end
      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
identity_cache-0.2.2 lib/identity_cache/fallback_fetcher.rb