Skip to content
This repository
Browse code

Merge pull request #3395 from bdurand/fix_file_store_cleanup

Fix ActiveSupport::Cache::FileStore.cleanup to actually work.
  • Loading branch information...
commit 79d01a8f16e20c556a086a2f07e3ccb4400f9819 2 parents b838570 + ec93f36
José Valim josevalim authored
21 activesupport/lib/active_support/cache/file_store.rb
@@ -26,11 +26,26 @@ def clear(options = nil)
26 26 FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)})
27 27 end
28 28
  29 + # Cleanup the cache by removing old entries. By default this will delete entries
  30 + # that haven't been accessed in one day. You can change this behavior by passing
  31 + # in a +not_accessed_in+ option. Any entry not accessed in that number of seconds
  32 + # in the past will be deleted. Alternatively, you can pass in +:expired_only+ with
  33 + # +true+ to only delete expired entries.
29 34 def cleanup(options = nil)
30 35 options = merged_options(options)
31   - each_key(options) do |key|
32   - entry = read_entry(key, options)
33   - delete_entry(key, options) if entry && entry.expired?
  36 + expired_only = options[:expired_only]
  37 + timestamp = Time.now - (options[:not_accessed_in] || 1.day.to_i)
  38 + search_dir(cache_path) do |fname|
  39 + if expired_only
  40 + key = file_path_key(fname)
  41 + entry = read_entry(key, options)
  42 + delete_entry(key, options) if entry && entry.expired?
  43 + else
  44 + if File.atime(fname) <= timestamp
  45 + key = file_path_key(fname)
  46 + delete_entry(key, options)
  47 + end
  48 + end
34 49 end
35 50 end
36 51
34 activesupport/test/caching_test.rb
@@ -557,6 +557,29 @@ def test_key_transformation_with_pathname
557 557 key = @cache_with_pathname.send(:key_file_path, "views/index?id=1")
558 558 assert_equal "views/index?id=1", @cache_with_pathname.send(:file_path_key, key)
559 559 end
  560 +
  561 + def test_cleanup_with_not_accessed_in
  562 + @cache.write(1, "aaaaaaaaaa")
  563 + @cache.write(2, "bbbbbbbbbb")
  564 + @cache.write(3, "cccccccccc")
  565 + sleep(2)
  566 + @cache.read(2)
  567 + @cache.cleanup(:not_accessed_in => 1)
  568 + assert_equal false, @cache.exist?(1)
  569 + assert_equal true, @cache.exist?(2)
  570 + assert_equal false, @cache.exist?(3)
  571 + end
  572 +
  573 + def test_cleanup_with_expired_only
  574 + @cache.write(1, "aaaaaaaaaa", :expires_in => 0.001)
  575 + @cache.write(2, "bbbbbbbbbb")
  576 + @cache.write(3, "cccccccccc", :expires_in => 0.001)
  577 + sleep(0.002)
  578 + @cache.cleanup(:expired_only => 0.001)
  579 + assert_equal false, @cache.exist?(1)
  580 + assert_equal true, @cache.exist?(2)
  581 + assert_equal false, @cache.exist?(3)
  582 + end
560 583
561 584 # Because file systems have a maximum filename size, filenames > max size should be split in to directories
562 585 # If filename is 'AAAAB', where max size is 4, the returned path should be AAAA/B
@@ -646,6 +669,17 @@ def @cache.delete_entry (*args)
646 669 assert_equal true, @cache.exist?(2)
647 670 assert_equal false, @cache.exist?(1)
648 671 end
  672 +
  673 + def test_cleanup_removes_expired_entries
  674 + @cache.write(1, "aaaaaaaaaa", :expires_in => 0.001)
  675 + @cache.write(2, "bbbbbbbbbb")
  676 + @cache.write(3, "cccccccccc", :expires_in => 0.001)
  677 + sleep(0.002)
  678 + @cache.cleanup
  679 + assert_equal false, @cache.exist?(1)
  680 + assert_equal true, @cache.exist?(2)
  681 + assert_equal false, @cache.exist?(3)
  682 + end
649 683 end
650 684
651 685 uses_memcached 'memcached backed store' do
6 railties/guides/source/caching_with_rails.textile
Source Rendered
@@ -289,7 +289,11 @@ ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
289 289
290 290 With this cache store, multiple server processes on the same host can share a cache. Servers processes running on different hosts could share a cache by using a shared file system, but that set up would not be ideal and is not recommended. The cache store is appropriate for low to medium traffic sites that are served off one or two hosts.
291 291
292   -Note that the cache will grow until the disk is full unless you periodically clear out old entries.
  292 +Note that the cache will grow until the disk is full unless you periodically clear out old entries. You can call +ActiveSupport::Cache::FileStore#cleanup+ to remove entries older than a specified time.
  293 +
  294 +<ruby>
  295 +Rails.cache.cleanup(:not_accessed_in => 2.days)
  296 +</ruby>
293 297
294 298 h4. ActiveSupport::Cache::MemCacheStore
295 299

0 comments on commit 79d01a8

Please sign in to comment.
Something went wrong with that request. Please try again.