Sha256: f5f9ea70923fe31e6861ba89b4b32a9ab8e26c096bcd20947094fb46c85ee0a5

Contents?: true

Size: 870 Bytes

Versions: 8

Compression:

Stored size: 870 Bytes

Contents

module RailsConnector

class Cache
  attr_reader :cache_prefix, :fallback_backend

  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, options={})
    @fallback_backend.write(cache_key_for_fallback_backend(key), value, options) if @fallback_backend
    @cache[key] = value
  end

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

  def clear
    @cache = {}
  end

  private

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

end # module RailsConnector

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
infopark_cloud_connector-7.1.0 lib/rails_connector/cache.rb
infopark_cloud_connector-7.0.2 lib/rails_connector/cache.rb
infopark_cloud_connector-7.0.1 lib/rails_connector/cache.rb
infopark_cloud_connector-7.0.0 lib/rails_connector/cache.rb
infopark_cloud_connector-6.9.5 lib/rails_connector/cache.rb
infopark_cloud_connector-6.9.4 lib/rails_connector/cache.rb
infopark_cloud_connector-6.9.3.1.36404185 lib/rails_connector/cache.rb
infopark_cloud_connector-6.9.2.1.125136549 lib/rails_connector/cache.rb