Can Rails' Fragment Caching be used without reference to persistent objects?

54 views Asked by At

The project I am working on is a console style application, and does not have access to any persistent objects at all. All our models are transient objects that extend Hashie::Mash and are populated on the fly via access to various API calls.

I'm looking into Rails fragment caching and the examples imply that the fragment caching mechanisms rely on the models being persistent.

I'm looking for examples of fragment caching that do not assume persistent models.

1

There are 1 answers

0
BBonifield On BEST ANSWER

Yes, Rails fragment caching can cache any old arbitrary thing. You just need to specify a key. For instance, this example from the docs:

<% cache('all_available_products') do %>
  All available products:
<% end %>

The problem you run into is that Rails isn't going to intelligently bust that cache because, as far as it's concerned, the data inside is static and would persist forever. And so, again from the docs, you'd have to bust the cache manually:

expire_fragment('all_available_products')

Now, the exact solution to your problem sort of depends on your use case. Perhaps you could bust the cache if something was different in an API call response or something similar. However to answer your question, yes, it's totally fine to use without persistent models.