Your idea is close, but you need to include shop_id , count and the maximum updated_at each store inventory in your cache key. Your external cache should be damaged when the store item is also deleted, and this does not apply only to max id or updated_at .
You can expand your own helper caching method to make this work. This allows you to create unique top-level caches that only go broke when a member of this set is added, updated, or deleted. Essentially, this provides a unique external cache for each shop_id . Thus, when one inventory store is changed, it does not affect another store cache.
Here is an example based on ideas in regional rail documentation :
module InventoriesHelper def cache_key_for_inventories(shop_id) count = Inventory.where(:shop_id => shop_id).count max_updated_at = Inventory.where(:shop_id => shop_id).maximum(:updated_at).try(:utc).try(:to_s, :number) "inventories/#{shop_id}-#{count}-#{max_updated_at}" end end
Then, in your opinion:
<% cache(cache_key_for_inventories(session[:shop_id])) do %> ... <% end %>
source share