Class: CmsContentSweeper

Inherits:
ActionController::Caching::Sweeper
  • Object
show all
Defined in:
app/sweepers/cms_content_sweeper.rb

Instance Method Summary collapse

Instance Method Details

#after_destroy(record) ⇒ Object



8
9
10
# File 'app/sweepers/cms_content_sweeper.rb', line 8

def after_destroy(record)
  delete_all_cached_pages
end

#after_save(record) ⇒ Object



4
5
6
# File 'app/sweepers/cms_content_sweeper.rb', line 4

def after_save(record)
  delete_all_cached_pages
end

#delete_all_cached_pagesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/sweepers/cms_content_sweeper.rb', line 12

def delete_all_cached_pages
  cache_dir = File.expand_path(Manage::CmsPagesController.page_cache_directory)
  public_dir = File.expand_path("#{Rails.root}/public")
  
  # this could throw Errno::ENOENT
  begin
    cache_dir = File.realpath cache_dir
    public_dir = File.realpath public_dir
  end
  
  begin
    if cache_dir == public_dir
      # expire home page
      expire_page controller: 'cms/content', action: 'show', content_path: nil
      
      # expire all other pages
      
      # original method is too slow for large sites:
      # CmsPage.select([ :id, :path ]).find_each do |page|
      #   expire_page controller: 'cms/content', action: 'show', content_path: page.path.split('/')
      # end
      
      # quicker method: remove entire directory trees when possible
      dangerous_names = [ 'assets', 'images', 'javascripts', 'media', 'stylesheets' ]
      home_page = CmsPage.find_by_path('')
      CmsPage.where(parent_id: home_page.id).each do |page|
        if dangerous_names.include?(page.path)
          # expire pages the old way
          Rails.logger.info "Expire tree #{path} using safe method"
          sub_page_paths(page).each do |path|
            expire_page controller: 'cms/content', action: 'show', content_path: path.split('/')
          end
        else
          # first clear this page
          expire_page controller: 'cms/content', action: 'show', content_path: page.path.split('/')
          
          # then attempt to remove entire directory tree, after sanity check
          path = File.expand_path(File.join(Manage::CmsPagesController.page_cache_directory, page.path))
          Dir.chdir Rails.root
          Dir.glob(Pathname.new(path).relative_path_from(Pathname.new(Rails.root)).to_s, File::FNM_CASEFOLD).each do |path|
            path = File.realpath(File.expand_path(path, Rails.root))
            if path && File.directory?(path) # could be nil if realpath threw an Errno::ENOENT
              raise "Cache directory name #{path} failed sanity check" unless path =~ /^#{public_dir}/
              Rails.logger.info "Expire tree #{path} using rm -rf"
              FileUtils.rm_rf(path, secure: true)
            end
          end
        end
      end
      
    else
      FileUtils.rm_r(Dir.glob("#{cache_dir}/*")) rescue Errno::ENOENT
    end
  rescue StandardError => e
    Rails.logger.error "Error while clearing cache: #{e.message}" unless e.is_a?(NoMethodError)
  end
end