Class: Scrivito::CacheGarbageCollector

Inherits:
Object
  • Object
show all
Defined in:
app/cms/scrivito/cache_garbage_collector.rb

Overview

The CacheGarbageCollector allows to remove old cache files from the Scrivito cache storage path. By default, a maximum of DEFAULT_MAX_FILE_INODES file inodes that were accessed lately are kept in the cache directory. All other files and empty directories are removed. Log output is written to the Rails environment log file.

Examples:

Running the garbage collector with default settings


bundle exec rake scrivito:cache:gc

Changing the maximum number of file inodes


bundle exec rake scrivito:cache:gc MAX_FILE_INODES=5000

Constant Summary collapse

DEFAULT_MAX_FILE_INODES =

Default maximum number of file inodes that are kept in the cache directory.

100000

Instance Method Summary collapse

Instance Method Details

#run_gcObject



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
69
70
71
72
# File 'app/cms/scrivito/cache_garbage_collector.rb', line 36

def run_gc
  announce "Cap number of cache file inodes at #{max_file_inodes} in #{cache_path}."

  directories = []
  file_inode_count = 0
  delta_disk_space = 0
  delta_count = 0

  cache_entries.each do |cache_entry|
    begin
      stat = File.stat(cache_entry)

      if stat.file?
        file_inode_count += 1
        next if file_inode_count <= max_file_inodes

        File.delete(cache_entry)

        delta_disk_space += stat.size
        delta_count += 1
      elsif stat.directory?
        directories << cache_entry
      end
    rescue Errno::ENOENT
    end
  end

  directories.sort.reverse_each do |dirname|
    begin
      Dir.rmdir(dirname)
      delta_count += 1
    rescue Errno::ENOTEMPTY, Errno::ENOENT
    end
  end

  announce "Done. Removed #{delta_count} entries. Saved at least #{delta_disk_space} Bytes."
end