Ticket #15 (new defect)

Opened 4 years ago

Cannot initialize MemCache object with array of hosts

Reported by: bleything Owned by: deveiant
Priority: major Milestone:
Component: Documentation Version:
Keywords: Cc:

Description

Sometimes you have an array of memcache server names in a variable that you want to use to initialize the MemCache object:

  caches = [ "foo1.example.com", "foo2.example.com" ]
  mc = MemCache.new( caches )

This will cause MemCache to think there is only one server. This happens because the first argument to the initializer is splatted, so you end up with a double array. The double array gets passed to #servers=, which runs collect on it (see line 313 of source:trunk/lib/memcache/memcache.rb). The collect block only gets run once, but it is passed the array that was originally passed to the initializer.

Whew.

It's possible to splat the argument going in to the constructor:

  caches = [ "foo1.example.com", "foo2.example.com" ]
  mc = MemCache.new( *caches )

This works most of the time, but fails when you need to send an option hash as well.

Another possible solution is flattening the array that's passed in, but this breaks the ability to specify weights in array form.

Note: See TracTickets for help on using tickets.