mperham / connection_pool

Generic connection pooling for Ruby
MIT License
1.63k stars 143 forks source link

read_multi / multi.set with dalli #45

Closed forrest closed 10 years ago

forrest commented 10 years ago

Hey,

I've been using dalli's read_multi and multi (combined with set) to try optimize our memcache. Both systems work with dalli directly.

We're now going to multithreading and trying to use connection_pool as you recommend in the readme for https://github.com/mperham/dalli, but we keep getting errors.

_readmulti raises: NoMethodError (undefined method 'split' for nil:NilClass)

Multi with set

Rails.cache.dalli.multi do
  array_of_items_to_write_back.each {|key, value|
    Rails.cache.dalli.set(key, value)
  }
end

raises `NoMethodError (undefined method 'multi' for #):``

Any tips on how to use connection_pool with our batch functionality?

Thanks

mperham commented 10 years ago

You can't reference the global as if it's a Dalli::Client instance. You must check out a connection from the pool via with and use it:

Rails.cache.dalli.with do |client|
  client.multi do
    array_of_items_to_write_back.each {|key, value|
      client.set(key, value)
    }
  end
end
mperham commented 10 years ago

And thanks for the feedback, good to see people playing with the new pool support.

forrest commented 10 years ago

Thanks @mperham for the quick response and the great gems. That makes sense for the multi set. Does this mean I need to wrap every spot I use Rails.cache.read(key) with the with block?

How about multi_read? I'm seeing this error: undefined method 'read_multi' for #<Dalli::Client:0x007faf5115ca18>)

Thanks again for all the help

mperham commented 10 years ago

No, any normal Rails.cache.method will implicitly use the pool automatically. You need to do this when you are trying to use dalli directly.

You need to show me the actual code so I know what you are doing wrong.

forrest commented 10 years ago

It seems like it was something weird with the memcached server I was running locally. Once I tried this in staging, everything worked like a dream. Thanks again for all the help.