Sha256: e1ab4038be9e8bf281e3c157e7e2599155d28b57c18187b96a3c3402502730b0

Contents?: true

Size: 1.24 KB

Versions: 7

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

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)
      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, **cache_fetcher_options)
      unless cache_fetcher_options.empty?
        raise ArgumentError, "unsupported cache_fetcher options: #{cache_fetcher_options.keys.join(", ")}"
      end

      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

7 entries across 7 versions & 1 rubygems

Version Path
identity_cache-1.5.1 lib/identity_cache/fallback_fetcher.rb
identity_cache-1.5.0 lib/identity_cache/fallback_fetcher.rb
identity_cache-1.4.1 lib/identity_cache/fallback_fetcher.rb
identity_cache-1.4.0 lib/identity_cache/fallback_fetcher.rb
identity_cache-1.3.1 lib/identity_cache/fallback_fetcher.rb
identity_cache-1.3.0 lib/identity_cache/fallback_fetcher.rb
identity_cache-1.2.0 lib/identity_cache/fallback_fetcher.rb