I am developing a ruby on rails application where I must get some
information from a third party application using HTTP request. This
information is seamlessly static (more less 30 minutes) so I would
like to get it once and use it during the next 30 minutes.
Is there an application scope (higher than session) where I can store
this information?
information is seamlessly static (more less 30 minutes) so I would
like to get it once and use it during the next 30 minutes.
Is there an application scope (higher than session) where I can store
this information?
There are different approaches to this. If you are running a single
rails server, then you can put the contents directly in a class
variable. If you are running in production mode, your class variables
will keep their values (until you change it or until you restart your
server). Keep in mind that if you stop your server, the next time you
are checking the variable will have nil value and you will have to ask
it from the remote site again.
If you are running a cluster of rails servers, then you can do one of
three things. One would be keeping the results in a file and then
reading the contents of the file each time. You could also keep the
contents in a file, and then load the contents of the file to a class
variable. In this case you would be loading a local copy in each of your
servers. These two approaches are feasible, but has some problems. One
is the file must be available from every rails server. If they are in
the same machine, all is fine, but if you are deploying over several
boxes, then you have to configure a shared directory. Another potential
problem is that, since you are caching contents locally for each server,
if you are talking about a big file, then you could be using a
significant amount of memory.
For multiple rails servers, I would recommend using a distributed cache,
such as memcached. That way, you only cache the contents once, and you
can access from any box as long as you have network connectivity between
them.
of course if what you are getting from a remote location is something
you are going to render directly in a view, you can just cache the
page/action and that would be all.
and if what you are getting is not something to render but values (like
quotes or prices, for example), then where i said “file” in my previous
mail, you can also read “db table”, which is probably easier.
I must keep information about a list of elements (similar to RSS), so
I think the best solution is a memory based one.
My problem was that using development environment the class variable
is always refreshed. I have changed to production mode and now the
value is kept between requests (I have done a mock test with
Time.now). What I must solve now is the refresh logic, is there any
solution to do that automatically? Something like a scheduler?.
Thanks so much
Juan
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.