Question

I have an application that uses caches_page for certain controllers/actions. To expire the cache, I use a sweeper. All in all, it's a standard solution.

However, because some changes may cause a bit of a rush of requests on the server (because push notifications are sent out and may trigger client devices to fetch new data), I'd like to be able to pre-render the cache, so it's ready before the requests roll in. I could just wait for the first request to automatically write the cache, of course, but in this case, I know that the requests will come, that there might be many, and that they may be near-simultaneous. So I'd like to have the cache ready.

To add some complexity, the updates are done via a normal web page and handled in a standard, mostly scaffolded controller, while the "page" I want to cache is the JSON response for an entirely different controller that serves as an API.

So, how do I, from a sweeper (or from the controller handling the cache-expiring update) trigger a new page cache to be written immediately?

Another way to put it might be: How do I make an internal request from one controller to another?


Edit: Ended up doing something like what you see below. It's not terribly elegant, but it is effective

class ModelSweeper < ActionController::Caching::Sweeper
  observe Model

  def after_create(model)
    expire_pages_for(model)
  end

  def after_update(model)
    expire_pages_for(model)
  end

  def after_destroy(model)
    expire_pages_for(model)
  end

  protected

    def expire_pages_for(model)
      # expire index page
      expire_and_bake(models_url)

      # expire show page
      expire_and_bake(model_url(model))
    end

    def expire_and_bake(url)
      # extract the path from the URL
      path = url.sub(%r{\Ahttp://[^/]+}, "")

      # expire the cache
      expire_page(path)

      # request the url (writes a new cache)
      system "curl '#{url}' &> /dev/null &"
    end

end

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top