Memcache-client for Ruby
Posted by Richard Tue, Jan 24 2006 21:42
A few days ago, there was a posting on the Ruby on Rails blog about a new memcache-client and cached_model. The instructions seemed straight forward so I thought I would give it a try. I added the CachedModel to a Rails application and had it working in no time. The application wasn't spread across multiple servers so there wasn't much point to it other than to test it. I decided I wanted to write a standalone ruby script to interact with my memcached server. While trying this, I ran into the following error:
memcache.rb:305:in `socket': uninitialized constant MemCache::Server::Timeout
After looking at the memcached.rb source code (a couple of times), I found that the Timeout class was not included in the memcached.rb file. It turned out to be an easy fix. I just added require 'timeout' to my script. Whether it should be included in the memcached.rb, I am not sure. Anyway, here is my script:
require 'rubygems'
require 'memcache'
require 'timeout' # had to require this for MemCache
CACHE = MemCache.new :c_threshold => 10_000,
:compression => true,
:debug => false,
:namespace => 'my_namespace_test',
:readonly => false,
:urlencode => false
CACHE.servers = ["127.0.0.1:11211"]
k = :key
v = CACHE.get k
if v.nil?
puts "setting value..."
v = "45678"
CACHE.set k, v, 300
else
puts "got value: #{v}"
end
puts "RESULT: #{k} => #{v}"
Once I added this, it worked without a problem. Maybe I will run some benchmarks next.
