viraptor
viraptor

Reputation: 34205

Caching in rails for the duration of a request

I've got a situation where I'd like to cache information from a static method, because the it's called many times.

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?

Upvotes: 1

Views: 670

Answers (1)

Siim Liiser
Siim Liiser

Reputation: 4368

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.

Upvotes: 4

Related Questions