Sha256: 50e025e31999d214bb9be40878d2b414a66d3447ec6cf860e02bd109ba2ae76e

Contents?: true

Size: 776 Bytes

Versions: 8

Compression:

Stored size: 776 Bytes

Contents

#:enddoc:
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

8 entries across 8 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.0.210.ed204b0 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.110.6570b45 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.72.d18d096 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.23.da7f96b lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.16.def5e85 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.15.a24f5ff lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.beta.200.891.647580e lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.0.beta.200.889.d503e42 lib/rails_connector/cache.rb