views:

137

answers:

3

I am using Ruby on Rails and I need to store a search result set obtained by connecting to another server. The problem is I don't want to store the result set in the session and I want something where I can store the result set object over multiple requests.

The querying takes time so I don't want to repeat it. Is there a way I could store objects or cache objects so that I don't have to query it again and again?

Can use some kind of object store?

Any help would be great.

If memoizing is an option how do I memoize objects? The connection would still take time so how to store the result set.

+1  A: 

If you don't want to store it in the session, obviously you have options here.

You can store in your DB temporarily (I assume querying the DB is faster than re-fetching from another server :)).

Also there's an option to use something like memcached. Although you have to be aware that restarting it will throw all your data away.

Depends on what you need to achieve and how you need to handle your data.

neutrino
+1  A: 

The memoization works only in single request. Can't be use in several request.

All request have all storage resource like Memcache or BDD.. If you use the ActiveSupport::Cache::MemCacheStore. you can push and fetch all object in all of your request.

shingara
+1  A: 

If you need to store data between requests, memoization likely isn't what you are looking for. Memoization is typically used in Ruby/Rails when you are calling the same method repeatedly within a single request, and that method is expensive (either CPU intensive, multiple DB requests, etc).

You can memoize a method, which stores the result in an instance variable, and the next time it is called, the instance variable value is returned, rather than reevaluating the method. There are tons of resources out there on this if you want to look into it further.

For data that needs to persist across sessions, and may need to be shared between different users, I highly recommend memcached. Rails has some built in support for it, so it shouldn't be too hard to dig up some good resources.

Beerlington