views:

431

answers:

5

Is there a way to persist data between page loads in RoR? I guess I'm looking for something like memcached, but without the overhead of TCP/IP. For example, PHP has APC.

The ideal solution would be something in memory.

+1  A: 

Why don't you just store it in the session? The session can have multiple backends like memcache or even a database. I think it is possible to deploy memcache locally so It wouldn't matter that much.

Another posibility is to use a file backend and store it on a RAM drive. But maybe there are some memory libs for ruby which allow you to store these results directly into ram, but I got no experience with it.

Tomh
A: 

How much data? If this is small, you could store it in the session data (i.e. session[:my_data] = "foo").

Keltia
+1  A: 

cookie based session stores are wicked fast, require no serverside storage or fetching, are secure, and the Rails default. As long as the data is less than 4K no reason not to just use that.

Squeegy
You don't want to use cookies for that, here is why: http://yuiblog.com/blog/2007/03/01/performance-research-part-3/
Tomh
Typically, the session contains very small bits of data, like a user ID. In these cases its trivial to send that along with the cookie the browser will be sending anyway. But I do agree that anything that needs more data than just that little bit should go through a server side store.
Squeegy
A: 

I wouldn't call the TCP/IP component "overhead" -- unless you are running the memcached server in another state or something. Memchached can be run locally just fine and rails works great with this. Perhaps memcached even has the ability to use a socket file instead of a IP and port, but I have not looked into this

bjeanes
A: 

You can also serialize ActiveRecord models. See the serialize method.

Chris Lee