Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Using sweepers to expire cache

  • Loading branch information...
commit 45c56384ee736fa2ef0b188ca6d2abbc33fde7fe 1 parent 73de0bf
Robert Huberdeau authored September 14, 2011
3  app/controllers/artworks_controller.rb
@@ -53,7 +53,6 @@ def create
53 53
         format.xml  { render :xml => @artwork.errors, :status => :unprocessable_entity }
54 54
       end
55 55
     end
56  
-    expire_fragment('top_nav')
57 56
   end
58 57
 
59 58
   # PUT /artworks/1
@@ -70,7 +69,6 @@ def update
70 69
         format.xml  { render :xml => @artwork.errors, :status => :unprocessable_entity }
71 70
       end
72 71
     end
73  
-    expire_fragment('top_nav')
74 72
   end
75 73
 
76 74
   # DELETE /artworks/1
@@ -84,5 +82,4 @@ def destroy
84 82
       format.xml  { head :ok }
85 83
     end
86 84
   end
87  
-  expire_fragment('top_nav')
88 85
 end
3  app/controllers/galleries_controller.rb
@@ -52,7 +52,6 @@ def create
52 52
         format.xml  { render :xml => @gallery.errors, :status => :unprocessable_entity }
53 53
       end
54 54
     end
55  
-    expire_fragment('top_nav')
56 55
   end
57 56
 
58 57
   # PUT /galleries/1
@@ -69,7 +68,6 @@ def update
69 68
         format.xml  { render :xml => @gallery.errors, :status => :unprocessable_entity }
70 69
       end
71 70
     end
72  
-    expire_fragment('top_nav')
73 71
   end
74 72
 
75 73
   # DELETE /galleries/1
@@ -82,6 +80,5 @@ def destroy
82 80
       format.html { redirect_to(galleries_url) }
83 81
       format.xml  { head :ok }
84 82
     end
85  
-    expire_fragment('top_nav')
86 83
   end
87 84
 end
15  app/sweepers/artwork_sweeper.rb
... ...
@@ -0,0 +1,15 @@
  1
+class ArtworkSweeper < ActionController::Caching::Sweeper
  2
+  observe Artwork
  3
+  
  4
+  def after_save(artwork)
  5
+    expire_cache(artwork)
  6
+  end
  7
+  
  8
+  def after_destroy(artwork)
  9
+    expire_cache(artwork)
  10
+  end
  11
+  
  12
+  def expire_cache(artwork)
  13
+    expire_fragment 'topnav'
  14
+  end
  15
+end
15  app/sweepers/gallery_sweeper.rb
... ...
@@ -0,0 +1,15 @@
1  config/application.rb
@@ -38,5 +38,6 @@ class Application < Rails::Application
38 38
 
39 39
     # Configure sensitive parameters which will be filtered from the log file.
40 40
     config.filter_parameters += [:password]
  41
+    config.load_paths << "#{RAILS_ROOT}/app/sweepers"
41 42
   end
42 43
 end

0 notes on commit 45c5638

Please sign in to comment.
Something went wrong with that request. Please try again.