Sha256: 8561d35ff922c0a63071278a5f4680a7701461124409a2c79199c6d3943eecb4

Contents?: true

Size: 1.04 KB

Versions: 10

Compression:

Stored size: 1.04 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_fill_cache?
    end

    def delete(key)
      @cache_backend.delete(key)
    end

    def clear
      @cache_backend.clear
    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_fill_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_fill_cache?
      end
      result
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
identity_cache-0.5.1 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.5.0 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.4.1 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.4.0 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.3.2 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.3.1 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.3.0 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.2.5 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.2.4 lib/identity_cache/fallback_fetcher.rb
identity_cache-0.2.3 lib/identity_cache/fallback_fetcher.rb