How to cache objects that store them for multiple requests?

I use Ruby on Rails, and I need to save the set of search results obtained when connecting to another server. The problem is that I don’t want to store the result set in the session, and I need something where I can store the result set object for several queries.

Requests take time, so I don’t want to repeat it. Is there a way to save objects or cache objects so I don't have to request it again and again?

Can I use some kind of storage object?

Any help would be great.

If memoizing is an option, how do I memoize objects? The connection will still take time to save the result set.

+4
source share
3 answers

If you do not want to store it in a session, obviously you have options here.

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

It is also possible to use something like memcached. Although you should be aware that a reboot will delete all your data.

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

+1
source

Demonization only works in one request. Unable to use multiple queries.

All requests have all storage resources, such as Memcache or BDD. If you are using ActiveSupport :: Cache :: MemCacheStore . You can click and get the whole object in all of your request.

+1
source

If you need to store data between requests, unmounting is probably not what you are looking for. Memoization is usually used in Ruby / Rails when calling the same method multiple times within the same query, and this method is expensive (processor intensity, several database queries, etc.).

You can memoize a method that stores the result in an instance variable, and the next call returns the value of the instance variable, rather than re-evaluating the method. There are tons of resources on this if you want to look into it further.

For data that should be stored in different sessions, and possibly need to be exchanged between different users, I highly recommend memcached. Rails has some built-in support for this, so it shouldn't be too hard to dig up some good resources.

+1
source

Source: https://habr.com/ru/post/1303664/


All Articles