After your comment I want to clear up a couple of things.
You should always be using fetch
if you require a result to come back. Wrap the call in a class method inside Book
for easy access:
class Book
def self.cached_books
Rails.cache.fetch < ... >
end
end
You can have a different method forcing the cache to be recreated:
def self.write_book_cache
Rails.cache.write < ... >
end
end
Then in your initializer, or in a rake task, you can just do:
Book.write_book_cache
This seems more maintainable to me, while keeping the succinct call to the cache in the rest of your code.