~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/cache/file_store.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module ActiveSupport
 
2
  module Cache
 
3
    # A cache store implementation which stores everything on the filesystem.
 
4
    class FileStore < Store
 
5
      attr_reader :cache_path
 
6
 
 
7
      def initialize(cache_path)
 
8
        @cache_path = cache_path
 
9
      end
 
10
 
 
11
      def read(name, options = nil)
 
12
        super
 
13
        File.open(real_file_path(name), 'rb') { |f| Marshal.load(f) } rescue nil
 
14
      end
 
15
 
 
16
      def write(name, value, options = nil)
 
17
        super
 
18
        ensure_cache_path(File.dirname(real_file_path(name)))
 
19
        File.atomic_write(real_file_path(name), cache_path) { |f| Marshal.dump(value, f) }
 
20
        value
 
21
      rescue => e
 
22
        logger.error "Couldn't create cache directory: #{name} (#{e.message})" if logger
 
23
      end
 
24
 
 
25
      def delete(name, options = nil)
 
26
        super
 
27
        File.delete(real_file_path(name))
 
28
      rescue SystemCallError => e
 
29
        # If there's no cache, then there's nothing to complain about
 
30
      end
 
31
 
 
32
      def delete_matched(matcher, options = nil)
 
33
        super
 
34
        search_dir(@cache_path) do |f|
 
35
          if f =~ matcher
 
36
            begin
 
37
              File.delete(f)
 
38
            rescue SystemCallError => e
 
39
              # If there's no cache, then there's nothing to complain about
 
40
            end
 
41
          end
 
42
        end
 
43
      end
 
44
 
 
45
      def exist?(name, options = nil)
 
46
        super
 
47
        File.exist?(real_file_path(name))
 
48
      end
 
49
 
 
50
      private
 
51
        def real_file_path(name)
 
52
          '%s/%s.cache' % [@cache_path, name.gsub('?', '.').gsub(':', '.')]
 
53
        end
 
54
 
 
55
        def ensure_cache_path(path)
 
56
          FileUtils.makedirs(path) unless File.exist?(path)
 
57
        end
 
58
 
 
59
        def search_dir(dir, &callback)
 
60
          Dir.foreach(dir) do |d|
 
61
            next if d == "." || d == ".."
 
62
            name = File.join(dir, d)
 
63
            if File.directory?(name)
 
64
              search_dir(name, &callback)
 
65
            else
 
66
              callback.call name
 
67
            end
 
68
          end
 
69
        end
 
70
    end
 
71
  end
 
72
end