Object Marshaling

Eric Hodel drbrain at segment7.net
Thu Feb 21 19:35:54 UTC 2008


On Feb 21, 2008, at 10:01 AM, Todd Calvert wrote:

> 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)

If you are sharing among ruby and non-ruby processes, don't store  
Marshaled objects, use raw mode get/set and use your own serialization  
scheme.

> 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)

No, unless Foo is a core ruby type (String, Array, Struct, etc)

> 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.

If foo.rb is so big that it is a burden to each client, your best bet  
would be to extract the data interchange portions and distribute those.

Alternately, you can make Foo a Struct, and have a separate file that  
re-opens the Foo class and adds your extra operations.

> memcache_options = {
>   :compression => true,
>   :debug => true,
>   :namespace=> 'todd_space',
>   :readonly => false,
>   :urlencode => false
>   }

Note that the compression, debug and urlencode options do nothing for  
memcache-client.  readonly is default.


More information about the memcached mailing list