I've got a situation where I'd like to cache information from a static method, because the it's called many times.
The information is only valid for the duration of the request, so I don't want to use Rails cache. (it's set to Redis which does more permanent caching)
I also can't easily bubble this up to any other reasonable owner. (there isn't one, this is a cross-cutting concern situation)
The only way I can think of is using a RequestStore
, but it seems like a pretty low-level solution. Is there anything else I can use here that's more integrated at Rails level?
CodePudding user response:
A solution I've used for this is ActiveSupport::CurrentAttributes. You can store data on it that will be reset on every new request. Though be aware that with it come most downsides of a global variable as you would expect.
Finding a proper owner and passing it along is the better option if you can manage to do that.