I have data that gets loaded from text files, then parsed into
specific data structures. I need this data to be persistent within the Rails
universe available to every request that comes into Rails.
I don't know if this is useful to you, but I did this:
yeah, I may end up with a proper class for the cache, meanwhile my main propblem is getting a variable to survive across multiple Rails page loads.
Here's a stripped down version of what I am doing.
if defined? $configCache == nil
$configCache = Hash.new
puts 'found in cache'
puts 'added to cache'
x = ConfigData.new
3.times do |i|
y = ConfigData.new
3.times do |i|
This works as expected with all accesses after the first one pulling from the global. Like I say, it's a cheap way to have a cache created on the fly dedicated to the needs of just this class.
If I put this into a rails project as /lib/config_data.rb (separated for ERB template, etc), I also get what I expect on the first pass. First load creates the global and populates the cache. After that the .getData pulls from the global.
The problem I am having is that when I reload the test page, I would expect to see _all_ .getData calls pull from the cache -- meaning the global is stil intact and still has data. Instead I see a repeat of the first pass. The global has to be created and populated, then subsequent .getData calls pull from the global.
This is where I'm puzzled, and where a global is either not a global, or the way Rails runs inside Ruby is working differently than I expected.
If I move the global declaration to environment.rb, I gets complaints from Rails that it was never declared -- so, whatever is going on there I think is the real problem.
Appreciate everyone chipping in so far. Thx.