Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails expire_page is not deleting the cached file

I have a controller action that has page caching, and I made a sweeper that calls expire_page with the controller and the action specified...

The controller action renders a js.erb template, so I am trying to ensure that expire_page deletes the .js file in public/javascripts, which it is not doing.

class JavascriptsController < ApplicationController

  caches_page :lol

  def lol
    @lol = Lol.all
  end

end

class LolSweeper < ActionController::Caching::Sweeper
  observe Lol

  def after_create(lol)
    puts "lol!!!!!!!"
    expire_page(:controller => "javascripts", :action => "lol", :format => 'js')
  end

end

... So, I visit javascripts/lol.js and I get my template rendered.. I verified that public/javascripts/lol.js exists... I then create a new Lol record, and I see "lol!!!!!!!!!" meaning the after_create observer method is triggered, but expire_page is doing nothing...

like image 208
patrick Avatar asked Oct 20 '11 01:10

patrick


1 Answers

According to RailsGuides: 'Page caching ignores all parameters.' I think I had similar problem while working on cashing .xml responses: I would write the cache for /lol.xml, but was trying to expire cache for /lol (write and expire operations can be seen in the server log). The way I made it work: I made the cache "format-agnostic" like this:

cashes_page :lol, :cache_path => Proc.new { |controller| controller.params.delete_if {|k,v| k == "format"} }

and expire in the sweeper like this:

expire_page(:controller => "javascripts", :action => "lol")

It solved my problem. Also, as a note, shouldn't your lol action be called lols? Good luck.

like image 166
Simon Bagreev Avatar answered Oct 25 '22 07:10

Simon Bagreev