Sha256: 784eb082b63dce9150e830cb9b53dab94bd5bf8d9723cb7793e264bd15336ce9

Contents?: true

Size: 1.78 KB

Versions: 10

Compression:

Stored size: 1.78 KB

Contents

require 'uri'
require 'rack/cache/meta_store'
require 'rack/cache/entity_store'

module Rack::Cache

  # Maintains a collection of MetaStore and EntityStore instances keyed by
  # URI. A single instance of this class can be used across a single process
  # to ensure that only a single instance of a backing store is created per
  # unique storage URI.
  class Storage
    def initialize
      @metastores = {}
      @entitystores = {}
    end

    def resolve_metastore_uri(uri, options = {})
      @metastores[uri.to_s] ||= create_store(MetaStore, uri, options)
    end

    def resolve_entitystore_uri(uri, options = {})
      @entitystores[uri.to_s] ||= create_store(EntityStore, uri, options)
    end

    def clear
      @metastores.clear
      @entitystores.clear
      nil
    end

  private

    def create_store(type, uri, options = {})
      if uri.respond_to?(:scheme) || uri.respond_to?(:to_str)
        uri = URI.parse(uri) unless uri.respond_to?(:scheme)
        if type.const_defined?(uri.scheme.upcase)
          klass = type.const_get(uri.scheme.upcase)
          return klass.resolve(uri) if klass.method(:resolve).arity == 1
          klass.resolve(uri, options)
        else
          fail "Unknown storage provider: #{uri.to_s}"
        end
      else
        # hack in support for passing a Dalli::Client or Memcached object
        # as the storage URI.
        case
        when defined?(::Dalli) && uri.kind_of?(::Dalli::Client)
          type.const_get(:Dalli).resolve(uri)
        when defined?(::Memcached) && uri.respond_to?(:stats)
          type.const_get(:MemCached).resolve(uri)
        else
          fail "Unknown storage provider: #{uri.to_s}"
        end
      end
    end

  public

    @@singleton_instance = new
    def self.instance
      @@singleton_instance
    end
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rack-cache-1.17.0 lib/rack/cache/storage.rb
rack-cache-1.16.0 lib/rack/cache/storage.rb
rack-cache-1.15.0 lib/rack/cache/storage.rb
rack-cache-1.14.0 lib/rack/cache/storage.rb
rack-cache-1.13.0 lib/rack/cache/storage.rb
rack-cache-1.12.1 lib/rack/cache/storage.rb
rack-cache-1.12.0 lib/rack/cache/storage.rb
rack-cache-1.11.1 lib/rack/cache/storage.rb
rack-cache-1.11.0 lib/rack/cache/storage.rb
rack-cache-1.10.0 lib/rack/cache/storage.rb