1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow MemCacheStore to be initialized with a MemCache object instead of addresses and options

This commit is contained in:
Bryan Helmkamp 2009-05-19 10:24:26 -04:00 committed by Jeremy Kemper
parent 6dc3a6a954
commit e2ed1a1ca4
3 changed files with 23 additions and 10 deletions

View file

@ -1,5 +1,7 @@
*Edge*
* Allow MemCacheStore to be initialized with a MemCache object instead of addresses and options [Bryan Helmkamp]
* Change spelling of Kyev timezone to Kyiv #2613 [Alexander Dymo]
* Add ActiveSupport.parse_json_times to disable time parsing in JSON backends that don't support it or don't need it. [rick]

View file

@ -23,7 +23,12 @@ module ActiveSupport
DELETED = "DELETED\r\n"
end
attr_reader :addresses
def self.build_mem_cache(*addresses)
addresses = addresses.flatten
options = addresses.extract_options!
addresses = ["localhost"] if addresses.empty?
MemCache.new(addresses, options)
end
# Creates a new MemCacheStore object, with the given memcached server
# addresses. Each address is either a host name, or a host-with-port string
@ -34,11 +39,11 @@ module ActiveSupport
# If no addresses are specified, then MemCacheStore will connect to
# localhost port 11211 (the default memcached port).
def initialize(*addresses)
addresses = addresses.flatten
options = addresses.extract_options!
addresses = ["localhost"] if addresses.empty?
@addresses = addresses
@data = MemCache.new(addresses, options)
if addresses.first.is_a?(MemCache)
@data = addresses.first
else
@data = self.class.build_mem_cache(*addresses)
end
extend Strategy::LocalCache
end

View file

@ -15,22 +15,28 @@ class CacheStoreSettingTest < ActiveSupport::TestCase
end
def test_mem_cache_fragment_cache_store
MemCache.expects(:new).with(%w[localhost], {})
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
assert_equal %w(localhost), store.addresses
end
def test_mem_cache_fragment_cache_store_with_given_mem_cache
mem_cache = MemCache.new
MemCache.expects(:new).never
store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
end
def test_mem_cache_fragment_cache_store_with_multiple_servers
MemCache.expects(:new).with(%w[localhost 192.168.1.1], {})
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
assert_equal %w(localhost 192.168.1.1), store.addresses
end
def test_mem_cache_fragment_cache_store_with_options
MemCache.expects(:new).with(%w[localhost 192.168.1.1], { :namespace => "foo" })
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo'
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
assert_equal %w(localhost 192.168.1.1), store.addresses
assert_equal 'foo', store.instance_variable_get('@data').instance_variable_get('@namespace')
end
def test_object_assigned_fragment_cache_store