Sha256: 779151d477cbbdf66bfad8fc2e1f06ea29648eca122c718f35b82304f1215065

Contents?: true

Size: 851 Bytes

Versions: 8

Compression:

Stored size: 851 Bytes

Contents

module RailsConnector

class Cache
  attr_reader :cache_prefix

  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-6.9.1.3.22208381 lib/rails_connector/cache.rb
infopark_cloud_connector-6.9.0.3.197272233 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.3.174.51542603 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.3.115.227021242 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.3.73.75172665 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.3.2.153416031 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.3.67.110847540 lib/rails_connector/cache.rb
infopark_cloud_connector-6.8.3.1.23895778 lib/rails_connector/cache.rb