I have a movie database with movies, genres, studios, actors, and a few more models.
The footer of the website shall always display something like "database contains x movies (y studios) with z genres and a actors". As I don't want to go and do "Movie.count", "Actor.count" etc on every single page refresh, I want to cache the values and update them only if a movie / actor etc is added or destroyed.
I thought of creating a method in the application controller that does something like the below (and then is called "before_action" application-wide), but that doesn't work - I got an error saying "unknown method 'cache_key_with_version', seems like this method is an ActiveRecord method, not an ActiveController one.
I was briefly thinking of using session variables to store these values then but that seems besides the design.
My draft controller method (that gave the error mentioned above):
def footer_cache
@footer_movies = Rails.cache.fetch("#{cache_key_with_version}/sum_movies", expires_in: 12.hours) do
Movie.count
end
@footer_genres = Rails.cache.fetch("#{cache_key_with_version}/sum_genres", expires_in: 12.hours) do
Genre.count
end
@footer_studios = Rails.cache.fetch("#{cache_key_with_version}/sum_studios", expires_in: 12.hours) do
Studio.count
end
@footer_actors = Rails.cache.fetch("#{cache_key_with_version}/sum_actors", expires_in: 12.hours) do
Actor.count
end
end
What am I missing?