Module: CarrierWave::Uploader::Cache::ClassMethods

Defined in:
lib/carrierwave/uploader/cache.rb

Instance Method Summary collapse

Instance Method Details

#clean_cached_files!(seconds = 60*60*24) ⇒ Object

Removes cached files which are older than one day. You could call this method from a rake task to clean out old cached files.

You can call this method directly on the module like this:

CarrierWave.clean_cached_files!

Note

This only works as long as you haven’t done anything funky with your cache_dir. It’s recommended that you keep cache files in one place only.



56
57
58
59
60
61
62
63
64
# File 'lib/carrierwave/uploader/cache.rb', line 56

def clean_cached_files!(seconds=60*60*24)
  Dir.glob(File.expand_path(File.join(cache_dir, '*'), CarrierWave.root)).each do |dir|
    time = dir.scan(/(\d+)-\d+-\d+/).first.map { |t| t.to_i }
    time = Time.at(*time)
    if time < (Time.now.utc - seconds)
      FileUtils.rm_rf(dir)
    end
  end
end