Sha256: d008995364d5aed5825a8d883505b4739afadfb6498a465beb69069a4bb8c440

Contents?: true

Size: 766 Bytes

Versions: 4

Compression:

Stored size: 766 Bytes

Contents

module RailsConnector

class Cache
  def initialize(options = {})
    @fallback_backend, @cache_prefix = options[:fallback_backend], options[:cache_prefix]
    @cache = {}
  end

  def read(key)
    value = @cache[key]
    if !value && @fallback_backend
      value = @fallback_backend.read(cache_key_for_fallback_backend(key))
      @cache[key] = value
    end
    value
  end

  def write(key, value)
    @fallback_backend.write(cache_key_for_fallback_backend(key), value) if @fallback_backend
    @cache[key] = value
  end

  def fetch(key, &block)
    read(key) || block.call.tap { |value| write(key, value) }
  end

  private

  def cache_key_for_fallback_backend(key)
    @cache_prefix ? "#{@cache_prefix}/#{key}" : key
  end
end

end # module RailsConnector

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.0.356.19698103 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.348.160665197 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.328.4a753fc lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.322.c003f11 lib/rails_connector/cache.rb