Class: PagesCore::CacheSweeper

Inherits:
Object
  • Object
show all
Defined in:
lib/pages_core/cache_sweeper.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir) ⇒ CacheSweeper

Returns a new instance of CacheSweeper.


75
76
77
# File 'lib/pages_core/cache_sweeper.rb', line 75

def initialize(cache_dir)
  @cache_dir = cache_dir
end

Class Attribute Details

.enabledObject

Returns the value of attribute enabled


4
5
6
# File 'lib/pages_core/cache_sweeper.rb', line 4

def enabled
  @enabled
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir


71
72
73
# File 'lib/pages_core/cache_sweeper.rb', line 71

def cache_dir
  @cache_dir
end

Class Method Details

.config {|@configuration| ... } ⇒ Object

Returns the configuration. Accepts a block, ie:

PagesCore::CacheSweeper.config do |c|
  c.patterns += [/^\/arkiv(.*)$/, /^\/tests(.*)$/]
end

Yields:


23
24
25
26
27
# File 'lib/pages_core/cache_sweeper.rb', line 23

def config
  @configuration ||= default_config
  yield @configuration if block_given?
  @configuration
end

.disable(&_block) ⇒ Object


6
7
8
9
10
11
# File 'lib/pages_core/cache_sweeper.rb', line 6

def disable(&_block)
  old_value = enabled
  self.enabled = false
  yield
  self.enabled = old_value
end

.once(&block) ⇒ Object


13
14
15
16
# File 'lib/pages_core/cache_sweeper.rb', line 13

def once(&block)
  disable(&block)
  PagesCore::SweepCacheJob.perform_later
end

.purge!Object

Purge the entire pages cache


30
31
32
33
# File 'lib/pages_core/cache_sweeper.rb', line 30

def purge!
  cache_dir = config.cache_path
  FileUtils.rm_rf(Dir.glob("#{cache_dir}/*")) if File.exist?(cache_dir)
end

.sweep!Object

Sweep all cached pages


36
37
38
39
# File 'lib/pages_core/cache_sweeper.rb', line 36

def sweep!
  return [] unless enabled
  cache_dirs.flat_map { |d| sweep_dir(d) }
end

Instance Method Details

#sweep!Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pages_core/cache_sweeper.rb', line 79

def sweep!
  return [] unless File.exist?(cache_dir)
  swept_files = []

  Find.find(cache_dir + "/") do |path|
    Find.prune if skip_path?(path) || !File.exist?(path)

    if sweep_file?(path)
      swept_files << path
      FileUtils.rm_rf(path)
    end
  end
  swept_files
end