Rails 7: implementing caching JSON responses in API controller leading to stale data
I'm performance testing and I'm a bit lost with Can someone help me understand I've been working on this all day and I'm working on a Rails 7 API application, and I've implemented caching for JSON responses using the `expires_in` method in my controller. However, I'm working with a question where the cached response does not seem to update when the underlying data changes, leading to stale data being served to clients. Here's a snippet of my controller: ```ruby class ProductsController < ApplicationController def index @products = Product.all expires_in 5.minutes, public: true render json: @products end end ``` The cache seems to work as expected initially, but when I create a new product or update an existing one, the old cached response is still being returned for the next few requests. I’ve tried manually clearing the cache in the `create` and `update` actions using `Rails.cache.clear`, but this approach seems inefficient and doesn't solve the question completely. I also considered using `cache_key` for the response, but I am unsure how to implement it effectively in this context. My cache store is configured to use Redis, which should ideally support this usage. I've looked into the Rails caching guide, but it only adds to my confusion regarding how to properly handle cache invalidation in this scenario. What is the best practice for ensuring that cached responses are updated when the underlying data changes? Are there specific configurations or methods to consider in Rails 7 to avoid serving stale data? Am I approaching this the right way? I'm developing on Windows 10 with Ruby. I've been using Ruby for about a year now. I'd be grateful for any help. I'm coming from a different tech stack and learning Ruby. Thanks for taking the time to read this!