2

Loosing ideas how to properly use action caching for a controller indirectly depending on a model.

The background scenario:

Having a simple rails model for product objects

class Product < ActiveRecord::Base
  ...
end

A controller which handles requests for feeds and uses Product instances to render views

class FeedsController < ActionController::Base
  # caches index action of a controller, generates large response
  cache_action :index

  def index
    @products = Product.all
    respond_to do |format|
      format.xml
    end
  end

  # need to clear cached `index' and fill cache again
  # PUT request is routed to `update'
  def update
    expire_action :action => :index, :format => :xml
    # ?? how call `index' again to update cache ??
    render nothing: true
  end
end

Now I'd need monitor changes in Product model and if any change is detected, make sure cached action from FeedsController gets invalidated and filled with updated data. Hope I'm asking nothing exotic.

So far I've ended with following observer:

class FeedSweeper < ActionController::Caching::Sweeper
  observe Product

  def after_update(product)
    # Not found other way than directly remove cached view
    # for FeedsController
    # No `@control' instance available here
    Rails.cache.delete_matched /feeds\/.+\.xml$/
    # How to invoke FeedsController#index from here ??
    # To fill updated data in cache
  end
end
  1. I cann't simply send GET request to fetch uncached feed because HTTP server throws timeout error which is hardcoded to 30 seconds
  2. I'm monitoring Product model alterations so there is no point to create callback for the FeedsController which is not connected to Product, just use its instances

Any advice how to monitor alterations in an independent model and update cache for other independent controller actions ?

David Unric
  • 7,421
  • 1
  • 37
  • 65

0 Answers0