I have a rails app with a simple class called Nutrients. This relates to a table with about 150 nutrient items and some small amounts of info. This data never changes during the apps use. As these items are used rather often, and the total size of data is tiny I really want to have them maintained constantly in memory (to reduce database or cache accesses unnecessarily).
As such I have looked at using a class memoization technique ie:
class Nutrient < ApplicationRecord
def self.all_cached
@@all_nutrients ||= Nutrient.all.to_a
end
end
Is this safe to do in a live rails app? I am concerned about thread safety and that potentially having separate processes access the same array could slow things down or cause crashes. This nutrient list is never changed or mutated after the app starts.