Permalink
Browse files

registration/resolution of custom store instances

  • Loading branch information...
1 parent 9cf8c0e commit c9be3a58592d2a73f871b908d1ead3d4898c3270 @tekwiz committed Sep 11, 2011
Showing with 66 additions and 26 deletions.
  1. +4 −4 lib/rack/cache/context.rb
  2. +34 −4 lib/rack/cache/storage.rb
  3. +28 −18 test/storage_test.rb
@@ -29,15 +29,15 @@ def initialize(backend, options={})
# The configured MetaStore instance. Changing the rack-cache.metastore
# value effects the result of this method immediately.
def metastore
- uri = options['rack-cache.metastore']
- storage.resolve_metastore_uri(uri)
+ uri_or_name = options['rack-cache.metastore']
+ storage.resolve_metastore(uri_or_name)
end
# The configured EntityStore instance. Changing the rack-cache.entitystore
# value effects the result of this method immediately.
def entitystore
- uri = options['rack-cache.entitystore']
- storage.resolve_entitystore_uri(uri)
+ uri_or_name = options['rack-cache.entitystore']
+ storage.resolve_entitystore(uri_or_name)
end
# The Rack call interface. The receiver acts as a prototype and runs
View
@@ -14,13 +14,23 @@ def initialize
@entitystores = {}
end
- def resolve_metastore_uri(uri)
- @metastores[uri.to_s] ||= create_store(MetaStore, uri)
+ def register_metastore(name, storage_or_uri)
+ register_store(@metastores, MetaStore, name, storage_or_uri)
end
- def resolve_entitystore_uri(uri)
- @entitystores[uri.to_s] ||= create_store(EntityStore, uri)
+ def resolve_metastore(uri_or_name)
+ resolve_store(@metastores, MetaStore, uri_or_name)
end
+ alias_method :resolve_metastore_uri, :resolve_metastore
+
+ def register_entitystore(name, storage_or_uri)
+ register_store(@entitystores, EntityStore, name, storage_or_uri)
+ end
+
+ def resolve_entitystore(uri_or_name)
+ resolve_store(@entitystores, EntityStore, uri_or_name)
+ end
+ alias_method :resolve_entitystore_uri, :resolve_entitystore
def clear
@metastores.clear
@@ -52,6 +62,26 @@ def create_store(type, uri)
end
end
+ def register_store(stores_hash, type, name, storage_or_uri)
+ if stores_hash[name.to_s]
+ raise ArgumentError, "%s already registered: %s" % [type, name]
+ end
+
+ if storage_or_uri.is_a?(type)
+ stores_hash[name.to_s] = storage_or_uri
+ else
+ stores_hash[name.to_s] = create_store(type, storage_or_uri)
+ end
+ end
+
+ def resolve_store(stores_hash, type, uri_or_name)
+ if stores_hash[uri_or_name.to_s]
+ stores_hash[uri_or_name.to_s]
+ else
+ register_store(stores_hash, type, uri_or_name, create_store(type, uri_or_name))
+ end
+ end
+
public
@@singleton_instance = new
def self.instance
View
@@ -7,40 +7,50 @@
end
it "fails when an unknown URI scheme is provided" do
- lambda { @storage.resolve_metastore_uri('foo:/') }.should.raise
+ lambda { @storage.resolve_metastore('foo:/') }.should.raise
end
it "creates a new MetaStore for URI if none exists" do
- @storage.resolve_metastore_uri('heap:/').
+ @storage.resolve_metastore('heap:/').
should.be.kind_of Rack::Cache::MetaStore
end
it "returns an existing MetaStore instance for URI that exists" do
- store = @storage.resolve_metastore_uri('heap:/')
- @storage.resolve_metastore_uri('heap:/').should.be.same_as store
+ store = @storage.resolve_metastore('heap:/')
+ @storage.resolve_metastore('heap:/').should.be.same_as store
end
it "creates a new EntityStore for URI if none exists" do
- @storage.resolve_entitystore_uri('heap:/').
+ @storage.resolve_entitystore('heap:/').
should.be.kind_of Rack::Cache::EntityStore
end
it "returns an existing EntityStore instance for URI that exists" do
- store = @storage.resolve_entitystore_uri('heap:/')
- @storage.resolve_entitystore_uri('heap:/').should.be.same_as store
+ store = @storage.resolve_entitystore('heap:/')
+ @storage.resolve_entitystore('heap:/').should.be.same_as store
end
it "clears all URI -> store mappings with #clear" do
- meta = @storage.resolve_metastore_uri('heap:/')
- entity = @storage.resolve_entitystore_uri('heap:/')
+ meta = @storage.resolve_metastore('heap:/')
+ entity = @storage.resolve_entitystore('heap:/')
@storage.clear
- @storage.resolve_metastore_uri('heap:/').should.not.be.same_as meta
- @storage.resolve_entitystore_uri('heap:/').should.not.be.same_as entity
+ @storage.resolve_metastore('heap:/').should.not.be.same_as meta
+ @storage.resolve_entitystore('heap:/').should.not.be.same_as entity
+ end
+ it "registers a MetaStore by name" do
+ store = Rack::Cache::MetaStore::Heap.new
+ @storage.register_metastore(:foo, store)
+ @storage.resolve_metastore(:foo).should.be.same_as store
+ end
+ it "registers an EntityStore by name" do
+ store = Rack::Cache::EntityStore::Heap.new
+ @storage.register_entitystore(:foo, store)
+ @storage.resolve_entitystore(:foo).should.be.same_as store
end
describe 'Heap Store URIs' do
%w[heap:/ mem:/].each do |uri|
it "resolves #{uri} meta store URIs" do
- @storage.resolve_metastore_uri(uri).
+ @storage.resolve_metastore(uri).
should.be.kind_of Rack::Cache::MetaStore
end
it "resolves #{uri} entity store URIs" do
- @storage.resolve_entitystore_uri(uri).
+ @storage.resolve_entitystore(uri).
should.be.kind_of Rack::Cache::EntityStore
end
end
@@ -57,11 +67,11 @@
%w[file: disk:].each do |uri|
it "resolves #{uri} meta store URIs" do
- @storage.resolve_metastore_uri(uri + @temp_dir).
+ @storage.resolve_metastore(uri + @temp_dir).
should.be.kind_of Rack::Cache::MetaStore
end
it "resolves #{uri} entity store URIs" do
- @storage.resolve_entitystore_uri(uri + @temp_dir).
+ @storage.resolve_entitystore(uri + @temp_dir).
should.be.kind_of Rack::Cache::EntityStore
end
end
@@ -73,18 +83,18 @@
%w[memcache: memcached:].each do |scheme|
it "resolves #{scheme} meta store URIs" do
uri = scheme + '//' + ENV['MEMCACHED']
- @storage.resolve_metastore_uri(uri).
+ @storage.resolve_metastore(uri).
should.be.kind_of Rack::Cache::MetaStore
end
it "resolves #{scheme} entity store URIs" do
uri = scheme + '//' + ENV['MEMCACHED']
- @storage.resolve_entitystore_uri(uri).
+ @storage.resolve_entitystore(uri).
should.be.kind_of Rack::Cache::EntityStore
end
end
it 'supports namespaces in memcached: URIs' do
uri = "memcached://" + ENV['MEMCACHED'] + "/namespace"
- @storage.resolve_metastore_uri(uri).
+ @storage.resolve_metastore(uri).
should.be.kind_of Rack::Cache::MetaStore
end
end

0 comments on commit c9be3a5

Please sign in to comment.