My question is about marshaling non-native Ruby objects in and out of Memcached. I would like to be able to take a user defined object (ex: foo) and put that into Memcached and then have another process retrieve that 'foo' object from Memcached without having the foo object definition. (the .rb file)

Is there a way to reconstitute the marshaled object when you have retrieved it from memcached if you don't have the object definition?(the .rb file)

I'm new to Memcached and Ruby, so any guidance would be appreciated. Here is the error I get when running the example code below. The foo object is inserted into memcached, but when trying to retrieve the foo object from memcached the foo object is undefined. I can get around this error if I require the foo object definition in get_foo.rb, but if I have distributed applications trying to retrieve this object I don't see it as optimal to make each client have the foo object definition.


[EMAIL PROTECTED]:~/code/ruby/memcache$ ruby get_foo.rb
/usr/lib/ruby/gems/1.8/gems/memcache-client-1.5.0/lib/memcache.rb:214:in `load': undefined class/module Foo (ArgumentError) from /usr/lib/ruby/gems/1.8/gems/memcache-client-1.5.0/lib/memcache.rb:214:in `get'
       from get_foo.rb:18



Example code:
--------------------------- foo.rb -----------------------------------------------------
#!/usr/bin/env ruby

class Foo
     attr_accessor :bar
       def initialize()
           @bar = 'bar'
       end
end
--------------------------- set_foo.rb ----------------------------------------------
!#/usr/bin/env ruby

require 'rubygems'
require 'memcache'
require 'foo'

memcache_options = {
   :compression => true,
   :debug => true,
   :namespace=> 'todd_space',
   :readonly => false,
   :urlencode => false
   }
Cache = MemCache.new memcache_options
   Cache.servers = '147.18.118.74:11211'
foo_object = Foo.new
   foo_object.bar = "foobar"
Cache.set('1', foo_object,0,false)
-----------------------------------------------------------------------------------------------
--------------------------------- get_foo.rb ------------------------------------------
!#/usr/bin/env ruby

require 'rubygems'
require 'memcache'


memcache_options = {
   :compression => true,
   :debug => true,
   :namespace=> 'todd_space',
   :readonly => false,
   :urlencode => false
   }
Cache = MemCache.new memcache_options
   Cache.servers = '147.18.118.74:11211'
obj = Cache.get('1') puts "Foo Object: bar value = #{obj.bar}"
--------------------------------------------------------------------------------------------

Thanks,
-Todd

Reply via email to