Memcache

Hi,

I am analysis memcache. Hence would like to know whats the best client for memcache. Have read a lot about memcache-client gem. Is that the most popular and widely used one?

Also most of my queries are complex queries and i have used find_by_sql for the same. Can i cache these results got from find_by_sql?

Thanks in advance.

After reading through a lot of blogs, everyone seems to say that memcache-client seems to be faster and better.

I installed memcache-client-1.8.5.gem

And my configuration in environment.rb file is:

memcache_options = {    :compression => true,    :debug => false,    :namespace => "mem-#{RAILS_ENV}",    :readonly => false,    :urlencode => false }

memcache_servers = [ '127.0.0.1:11211' ]

CACHE = MemCache.new(memcache_options) CACHE.servers = memcache_servers

However, evrytime i do a get to memcache in the memcache console i see it being retrieved, however i get error while displaying it.

The logs show : MemCache::MemCacheError (Resource temporarily unavailable - ):   c:/jruby-1.5.1/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'   app/controllers/application_controller.rb:102:in `data_cache'   app/controllers/channel_members_controller.rb:27:in `follows'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:162:in `start'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:95:in `start'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:92:in `each'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:92:in `start'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:23:in `start'   c:/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:82:in `start'

Can anyone help what is happening?

Yani Yen wrote:

Quoting Yani Yen <lists@ruby-forum.com>:

After reading through a lot of blogs, everyone seems to say that memcache-client seems to be faster and better.

I installed memcache-client-1.8.5.gem

And my configuration in environment.rb file is:

memcache_options = {    :compression => true,    :debug => false,    :namespace => "mem-#{RAILS_ENV}",    :readonly => false,    :urlencode => false }

memcache_servers = [ '127.0.0.1:11211' ]

CACHE = MemCache.new(memcache_options) CACHE.servers = memcache_servers

In my application, all I do is add the following line to conf/environments/production.rb and restart the server:

config.cache_store = :mem_cache_store

I only cache in production, caching in development is too error prone, I change something and am getting the incorrect cached result instead of the new corrected result.

The host and port defaults to 127.0.0.1:11211. Start here, when that works, get fancier, e.g.:

config.cache_store = :mem_cache_store, '127.0.0.1:11211', {   :compression => true,   :debug => false,   :namespace => "mem-#{RAILS_ENV}",   :readonly => false,   :urlencode => false }

HTH,   Jeffrey

Quoting Yani Yen <lists@ruby-forum.com>:

Forgot to mention :

I had the configuration : config.cache_store = :mem_cache_store in my specific environment file. Still i was getting the above mentioned error

Yani Yen wrote: > Hi, > > What i did in my environment.rb file was created an instance of memcache > and assign it to a constant: > > CACHE = MemCache.new('127.0.0.1') > > and then in my code something like this:

[snip]

Okay, that makes it clearer to me. I think you are starting two instances of the cache client and the second one fails because the first is already present and running. Try with just the

config.cache_store = :mem_cache_store

and comment out the rest.

Jeffrey

Hi Jeffrey,

I kept the folowing configuration only: config.cache_store = :mem_cache_store and it works.

But now i want to try other way that is creating an instance like this:

memcache_options = {    :compression => true,    :debug => false,    :namespace => "mem-#{RAILS_ENV}",    :readonly => false,    :urlencode => false }

memcache_servers = [ '127.0.0.1:11211' ]

CACHE = MemCache.new(memcache_options) CACHE.servers = memcache_servers

and i commented out the following: config.cache_store = :mem_cache_store

But during server startup it throws the following error:

=> Booting WEBrick => Rails 2.3.5 application starting on http://0.0.0.0:3000 C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/activesupport/lib/active_support/dependencies.rb:443:in ` load_missing_constant': uninitialized constant MemCache (NameError)         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/activesupport/lib/active_support/dependencie s.rb:80:in `const_missing_with_dependencies'         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/activesupport/lib/active_support/dependencie s.rb:92:in `const_missing'         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/config/environment.rb:62         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/config/environment.rb:31:in `require'         from c:/jruby-1.5.1/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/activesupport/lib/active_support/dependencie s.rb:156:in `require'         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/activesupport/lib/active_support/dependencie s.rb:521:in `new_constants_in'         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/activesupport/lib/active_support/dependencie s.rb:156:in `require'         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/railties/lib/commands/server.rb:84         from C:/WEB2.0/Workspaces/RELEASE 2/Version 1/microblogging/vendor/rails/railties/lib/commands/server.rb:31:in `requi re'         from c:/jruby-1.5.1/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'         from script/server:3

How do i create a constant which is an instance of memcache, as i am trying above?

Jeffrey L. Taylor wrote:

You are fighting Rails, in my experience not a time effective thing to do. Why do you want use Rails and not do it the Rails way?

Jeffrey

Quoting Prachi Tripathi <lists@ruby-forum.com>:

Hi,

I guess you write. I used memcache the rails way.

I have one more doubt. I want a write through cache mechanism. How to enable that?

Is there some configuration?

Also, found that cache-money is a write through as well as read only cache. I wanted to know how to configure it to behave as write through ie, any new entry should first be made to cache and then to the backing store. In my case the database.

The below configuration: :readonly => false, makes cache write enabled, but does it also write it to the backing store?

I want a write through cache policy

Jeffrey L. Taylor wrote:

Prachi,   I can't help you there, I've used memcache only for caching HTML fragments. There are several fine tutorials on using memcache on the Web, including using it to cache database records. Try railscasts.com, their screencasts are excellent.

Jeffrey

Quoting Prachi Tripathi <lists@ruby-forum.com>: