public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
refactored cache sweeping methods

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2275 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Mon Sep 25 20:07:27 -0700 2006
commit  4231359d8ccbbff9cd7436832d479b8a13859aa2
tree    711fad4e2862e73b143caa0aaf22a7d89473efd7
parent  17063ca5b25cb3d05b6c38d2909f1be3ae0fbb20
...
9
10
11
12
13
 
14
15
16
...
9
10
11
 
 
12
13
14
15
0
@@ -9,8 +9,7 @@ class ArticleSweeper < ActionController::Caching::Sweeper
0
   def after_save(record)
0
     return if controller.nil?
0
     expire_overview_feed! if record.is_a?(Article)
0
- pages = CachedPage.find_by_reference(record)
0
- expire_cached_pages "Expired pages referenced by #{record.class} ##{record.id}", *pages
0
+ site.expire_cached_pages controller, "Expired pages referenced by #{record.class} ##{record.id}", site.cached_pages.find_by_reference(record)
0
   end
0
 
0
   alias after_destroy after_save
...
3
4
5
6
7
 
8
9
10
11
...
3
4
5
 
 
6
7
8
9
10
0
@@ -3,8 +3,7 @@ class AssetSweeper < ActionController::Caching::Sweeper
0
   #observe Resource
0
   def after_save(record)
0
     return if controller.nil?
0
- pages = CachedPage.find_by_reference(record)
0
- expire_cached_pages "Expired pages referenced by #{record.class} ##{record.id}", *pages
0
+ site.expire_cached_pages controller, "Expired pages referenced by #{record.class} ##{record.id}", site.cached_pages.find_by_reference(record)
0
   end
0
   alias after_destroy after_save
0
 end
0
\ No newline at end of file
...
4
5
6
7
8
 
 
9
10
11
...
4
5
6
 
 
7
8
9
10
11
0
@@ -4,8 +4,8 @@ class AssignedSectionSweeper < ActionController::Caching::Sweeper
0
 
0
   def after_destroy(record)
0
     return if controller.nil?
0
- pages = CachedPage.find_by_reference_key('Section', record.section_id)
0
- expire_cached_pages "Expired pages referenced by Section ##{record.section_id}", *pages
0
+ pages = site.cached_pages.find_by_reference_key('Section', record.section_id)
0
+ site.expire_cached_pages controller, "Expired pages referenced by Section ##{record.section_id}", pages
0
   end
0
   
0
   alias after_create after_destroy
...
22
23
24
25
26
 
 
27
28
29
...
22
23
24
 
 
25
26
27
28
29
0
@@ -22,7 +22,7 @@ class CommentSweeper < ActionController::Caching::Sweeper
0
   
0
   protected
0
     def expire_cache_for(comment)
0
- pages = CachedPage.find_by_references(comment, comment.article)
0
- expire_cached_pages "Expired pages referenced by #{comment.class} ##{comment.id}", *pages
0
+ pages = site.cached_pages.find_by_references(comment, comment.article)
0
+ site.expire_cached_pages controller, "Expired pages referenced by #{comment.class} ##{comment.id}", pages
0
     end
0
 end
0
\ No newline at end of file
...
9
10
11
12
 
13
14
15
...
9
10
11
 
12
13
14
15
0
@@ -9,6 +9,6 @@ class TemplateSweeper < ActionController::Caching::Sweeper
0
   end
0
 
0
   def after_save(record)
0
- expire_cached_pages "Expired all referenced pages", *CachedPage.find(:all) if @new.nil? && controller
0
+ site.expire_cached_pages controller, "Expired all referenced pages" if @new.nil? && controller
0
   end
0
 end
0
\ No newline at end of file
...
16
17
18
19
 
20
21
22
...
16
17
18
 
19
20
21
22
0
@@ -16,7 +16,7 @@ class Admin::TemplatesController < Admin::DesignController
0
 
0
   def update
0
     @theme.templates.write(params[:filename], params[:data])
0
- Mephisto::SweeperMethods.expire_cached_pages "Expired all referenced pages", self, *CachedPage.find(:all) if current_theme?
0
+ site.expire_cached_pages self, "Expired all referenced pages" if current_theme?
0
     render :update do |page|
0
       page.call 'Flash.notice', 'Template updated successfully'
0
     end
...
79
80
81
82
 
83
84
85
...
79
80
81
 
82
83
84
85
0
@@ -79,7 +79,7 @@ class Admin::ThemesController < Admin::BaseController
0
     end
0
 
0
     def sweep_cache
0
- Mephisto::SweeperMethods.expire_cached_pages "Expired all referenced pages", self, *CachedPage.find(:all)
0
+ site.expire_cached_pages self, "Expired all referenced pages"
0
     end
0
 
0
     alias authorized? admin?
...
64
65
66
67
68
69
 
70
71
72
...
64
65
66
 
 
 
67
68
69
70
0
@@ -64,9 +64,7 @@ class ApplicationController < ActionController::Base
0
     def set_cache_root
0
       host = request.domain(request.subdomains.size + (request.subdomains.first == 'www' ? 0 : 1))
0
       @site ||= Site.find_by_host(host) || Site.find(:first, :order => 'id')
0
- if @site.multi_sites_enabled
0
- self.class.page_cache_directory = File.join([RAILS_ROOT, (RAILS_ENV == 'test' ? 'tmp' : 'public'), 'cache', site.host])
0
- end
0
+ self.class.page_cache_directory = site.page_cache_directory.to_s
0
     end
0
 
0
     def with_site_timezone
...
18
19
20
 
 
 
 
21
22
23
...
32
33
34
35
36
37
 
38
39
40
...
18
19
20
21
22
23
24
25
26
27
...
36
37
38
 
 
 
39
40
41
42
0
@@ -18,6 +18,10 @@ class CachedPage < ActiveRecord::Base
0
       with_scope current_scope_conditions, &block
0
     end
0
 
0
+ def find_current(*args)
0
+ with_current_scope { find(*args) }
0
+ end
0
+
0
     # Finds all pages that this record refers to
0
     #
0
     # CachedPage.find_by_reference Foo.find(15)
0
@@ -32,9 +36,7 @@ class CachedPage < ActiveRecord::Base
0
     # CachedPage.find_by_reference_keys ['Foo', 15], ['Bar', 17]
0
     #
0
     def find_by_reference_keys(*array_of_keys)
0
- with_current_scope do
0
- find :all, :conditions => ["(#{array_of_keys.collect { |r| "#{connection.quote_column_name('references')} LIKE ?" } * ' OR '})", *array_of_keys.collect { |r| "%[#{[r.last, r.first] * ':'}]%" }]
0
- end
0
+ find_current :all, :conditions => ["(#{array_of_keys.collect { |r| "#{connection.quote_column_name('references')} LIKE ?" } * ' OR '})", *array_of_keys.collect { |r| "%[#{[r.last, r.first] * ':'}]%" }]
0
     end
0
 
0
     # Finds all pages that this record refers to
...
2
3
4
5
 
6
7
8
...
195
196
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
 
 
 
199
200
201
...
2
3
4
 
5
6
7
8
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
0
@@ -2,7 +2,7 @@ class Site < ActiveRecord::Base
0
   @@theme_path = Pathname.new(RAILS_ROOT) + 'themes'
0
   cattr_reader :theme_path
0
 
0
- cattr_accessor :multi_sites_enabled
0
+ cattr_accessor :multi_sites_enabled, :cache_sweeper_tracing
0
 
0
   has_many :sections do
0
     def home
0
@@ -195,7 +195,26 @@ class Site < ActiveRecord::Base
0
     original_timezone_writer(name)
0
   end
0
 
0
+ def page_cache_directory
0
+ multi_sites_enabled ?
0
+ (RAILS_PATH + (RAILS_ENV == 'test' ? 'tmp' : 'public') + 'cache' + host) :
0
+ (RAILS_PATH + (RAILS_ENV == 'test' ? 'tmp/cache' : 'public'))
0
+ end
0
+
0
+ def expire_cached_pages(controller, log_message, pages = nil)
0
+ pages ||= cached_pages.find_current(:all)
0
+ returning cached_log_message_for(log_message, pages) do |msg|
0
+ controller.logger.warn msg if cache_sweeper_tracing
0
+ pages.each { |p| controller.class.expire_page(p.url) }
0
+ CachedPage.expire_pages(self, pages)
0
+ end
0
+ end
0
+
0
   protected
0
+ def cached_log_message_for(log_message, pages)
0
+ pages.inject([log_message, "Expiring #{pages.size} page(s)"]) { |msg, p| msg << " - #{p.url}" }.join("\n")
0
+ end
0
+
0
     def permalink_variable_format?(var)
0
       Mephisto::Dispatcher.variable_format?(var)
0
     end
...
44
45
46
47
 
48
49
50
...
44
45
46
 
47
48
49
50
0
@@ -44,7 +44,7 @@ require 'mephisto_init'
0
 # ActionController::AbstractRequest.relative_url_root = '/blog'
0
 
0
 # turn this on to get detailed cache sweeper logging in production mode
0
-# Mephisto::SweeperMethods.cache_sweeper_tracing = true
0
+# Site.cache_sweeper_tracing = true
0
 
0
 # Enable if you want to host multiple sites on this app
0
 # Site.multi_sites_enabled = true
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
25
26
27
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
5
6
7
0
@@ -1,27 +1,7 @@
0
 module Mephisto
0
   module SweeperMethods
0
- mattr_accessor :cache_sweeper_tracing
0
-
0
- def self.expire_cached_pages(log_message, controller, *pages)
0
- if cache_sweeper_tracing
0
- controller.logger.warn log_message
0
- controller.logger.warn "Expiring #{pages.size} page(s)"
0
- pages.each do |page|
0
- controller.logger.warn " - #{page.url}"
0
- end
0
- end
0
- if pages.any?
0
- pages.each { |p| controller.class.expire_page(p.url) }
0
- CachedPage.expire_pages(controller.site, pages)
0
- end
0
- end
0
-
0
- def expire_cached_pages(log_message, *pages)
0
- SweeperMethods.expire_cached_pages(log_message, controller, *pages)
0
- end
0
-
0
     def expire_overview_feed!
0
- if cache_sweeper_tracing
0
+ if Site.cache_sweeper_tracing
0
         controller.logger.warn "Expiring Overview Feed: #{overview_path}"
0
       end
0
       controller.class.expire_page overview_path
...
7
8
9
 
 
10
11
12
...
7
8
9
10
11
12
13
14
0
@@ -7,6 +7,8 @@ require File.expand_path(File.dirname(__FILE__) + "/actor")
0
 ASSET_PATH = File.join(RAILS_ROOT, 'test/fixtures/tmp/assets') unless Object.const_defined?(:ASSET_PATH)
0
 require File.join(File.dirname(__FILE__), 'referenced_caching_test_helper')
0
 
0
+Site.cache_sweeper_tracing = true
0
+
0
 Time.class_eval do
0
   class << self
0
     alias_method :real_now, :now

Comments

    No one has commented yet.