I am creating a web-based IMAP client in Rails, and want to cache the IMAP object between requests. I was planning to serialize the object and save it in Redis (since it caches strings only), however none of the popular serialization methods seems to be working.
Marshal and ActiveSupport::Cache::MemoryStore both give the following error
Marshal.dump(imap)
TypeError: no marshal_dump is defined for class Mutex
YAML serialization works, but deserializing fails.
s = YAML::dump(imap) # works, i.e. loads up a string with the imap data
imap2 = YAML::load(s)
TypeError: allocator undefined for Thread
Is there any other alternate caching mechanism that works for arbitrary ruby objects, especially ones that might be using threads internally? Does an alternate key-value store (I have been using Redis) support such caching? Even better, is there anyway for Rails to remember certain objects, rather than me deser开发者_运维技巧ializing them?
PS> I am using Ruby 1.9.2 with Rails 3.0.3 on a Macbook, if that helps in any way.
Serializing only works for data objects (or part of the data of an object). Network connections can't be serialized, since they are very specific to a client, server and time (by default a tcp connection times out after 5 minutes). You could use a global IMAP connection that you put in memory within your application, but you might run into issues there with several users. The most standard way is to have a pool of connections where you borrow a connection from. By the way, are you running into issues when creating a connection every time? I would not cache the connection, but the data that you retrieved via IMAP and work from there.
精彩评论