Application Context in Rails

Rails comes with a convenient hash session in which we can squeeze stuff into our hearty content. However, I would like something like the context of an ASP application that, instead of sharing data for only one session, will share it with all sessions in one application. I am writing a simple toolbar application and would like to retrieve data every 5 minutes, not every 5 minutes for each session.

I could, of course, store the cache update times in the database, but so far there was no need to configure the database for this application and I would like to avoid this dependency, if possible.

So, is there a way to get (or simulate) such things? If it is not possible to do this without a database, is there any “fake” database engine that comes with Rails that works in memory but does not bother current data between reboots?

+3
source share
7 answers

Correct answer : memcached. Fast, clean, supports several processes, now it is very clean using Rails. It's not even that bad to set up, but that’s another thing to keep working.

90% . , Rails - Mongrel, , . , Mongrel . , , ,

  • 8 ​​.
  • , 20 000 .
  • 4 (Mongrels)

20 000 12

@@arbitrary_name ||= Model.find_by_stupidly_long_query(param)

at, Ruby, , , , . || = - Ruby , false. , , - - , , .

, 20 . 12 15 (, - , ), , 4 .

, , (.. ). , , , SQL ).

, , , 90% , .

+7

Railscast Rails 2.1. , memcached Rails.

+2

Rails .

+1

@p3t0r- , MemCached, , , sqlite, Rails. , MemCached . , sqlite , , , . Rails , , , ASP.NET Java.

0

What you ask for is completely impossible in Rails because of how it is designed. What you are asking for is a shared object, and Rails is strictly single-threaded. Memcached or a similar tool for exchanging data between distributed processes is the only way to go.

0
source

Rails.cache freezes stored objects. This approach makes sense for the cache, but NOT for the application context. I assume that instead of going to the moon to complete this simple task, all you have to do is create a constant inside config / environment.rb

APP_CONTEXT = Hash.new

Pretty simple, huh?

0
source

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


All Articles