Class Index [+]

Quicksearch

ActiveSupport::Cache::FileStore

A cache store implementation which stores everything on the filesystem.

FileStore implements the Strategy::LocalCache strategy which implements an in memory cache inside of a block.

Constants

DIR_FORMATTER
ESCAPE_FILENAME_CHARS
UNESCAPE_FILENAME_CHARS

Attributes

cache_path[R]

Public Class Methods

new(cache_path, options = nil) click to toggle source
    # File lib/active_support/cache/file_store.rb, line 17
17:       def initialize(cache_path, options = nil)
18:         super(options)
19:         @cache_path = cache_path
20:         extend Strategy::LocalCache
21:       end

Public Instance Methods

cleanup(options = nil) click to toggle source
    # File lib/active_support/cache/file_store.rb, line 28
28:       def cleanup(options = nil)
29:         options = merged_options(options)
30:         each_key(options) do |key|
31:           entry = read_entry(key, options)
32:           delete_entry(key, options) if entry && entry.expired?
33:         end
34:       end
clear(options = nil) click to toggle source
    # File lib/active_support/cache/file_store.rb, line 23
23:       def clear(options = nil)
24:         root_dirs = Dir.entries(cache_path).reject{|f| ['.', '..'].include?(f)}
25:         FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)})
26:       end
decrement(name, amount = 1, options = nil) click to toggle source
    # File lib/active_support/cache/file_store.rb, line 50
50:       def decrement(name, amount = 1, options = nil)
51:         file_name = key_file_path(namespaced_key(name, options))
52:         lock_file(file_name) do
53:           options = merged_options(options)
54:           if num = read(name, options)
55:             num = num.to_i - amount
56:             write(name, num, options)
57:             num
58:           else
59:             nil
60:           end
61:         end
62:       end
delete_matched(matcher, options = nil) click to toggle source
    # File lib/active_support/cache/file_store.rb, line 64
64:       def delete_matched(matcher, options = nil)
65:         options = merged_options(options)
66:         instrument(:delete_matched, matcher.inspect) do
67:           matcher = key_matcher(matcher, options)
68:           search_dir(cache_path) do |path|
69:             key = file_path_key(path)
70:             delete_entry(key, options) if key.match(matcher)
71:           end
72:         end
73:       end
increment(name, amount = 1, options = nil) click to toggle source
    # File lib/active_support/cache/file_store.rb, line 36
36:       def increment(name, amount = 1, options = nil)
37:         file_name = key_file_path(namespaced_key(name, options))
38:         lock_file(file_name) do
39:           options = merged_options(options)
40:           if num = read(name, options)
41:             num = num.to_i + amount
42:             write(name, num, options)
43:             num
44:           else
45:             nil
46:           end
47:         end
48:       end

Protected Instance Methods

delete_entry(key, options) click to toggle source
     # File lib/active_support/cache/file_store.rb, line 105
105:         def delete_entry(key, options)
106:           file_name = key_file_path(key)
107:           if File.exist?(file_name)
108:             begin
109:               File.delete(file_name)
110:               delete_empty_directories(File.dirname(file_name))
111:               true
112:             rescue => e
113:               # Just in case the error was caused by another process deleting the file first.
114:               raise e if File.exist?(file_name)
115:               false
116:             end
117:           end
118:         end
read_entry(key, options) click to toggle source
    # File lib/active_support/cache/file_store.rb, line 77
77:         def read_entry(key, options)
78:           file_name = key_file_path(key)
79:           if File.exist?(file_name)
80:             entry = File.open(file_name) { |f| Marshal.load(f) }
81:             if entry && !entry.expired? && !entry.expires_in && !self.options[:expires_in]
82:               # Check for deprecated use of +:expires_in+ option from versions < 3.0
83:               deprecated_expires_in = options[:expires_in]
84:               if deprecated_expires_in
85:                 ActiveSupport::Deprecation.warn('Setting :expires_in on read has been deprecated in favor of setting it on write.', caller)
86:                 if entry.created_at + deprecated_expires_in.to_f <= Time.now.to_f
87:                   delete_entry(key, options)
88:                   entry = nil
89:                 end
90:               end
91:             end
92:             entry
93:           end
94:         rescue
95:           nil
96:         end
write_entry(key, entry, options) click to toggle source
     # File lib/active_support/cache/file_store.rb, line 98
 98:         def write_entry(key, entry, options)
 99:           file_name = key_file_path(key)
100:           ensure_cache_path(File.dirname(file_name))
101:           File.atomic_write(file_name, cache_path) {|f| Marshal.dump(entry, f)}
102:           true
103:         end

Private Instance Methods

delete_empty_directories(dir) click to toggle source

Delete empty directories in the cache.

     # File lib/active_support/cache/file_store.rb, line 163
163:         def delete_empty_directories(dir)
164:           return if dir == cache_path
165:           if Dir.entries(dir).reject{|f| ['.', '..'].include?(f)}.empty?
166:             File.delete(dir) rescue nil
167:             delete_empty_directories(File.dirname(dir))
168:           end
169:         end
ensure_cache_path(path) click to toggle source

Make sure a file path’s directories exist.

     # File lib/active_support/cache/file_store.rb, line 172
172:         def ensure_cache_path(path)
173:           FileUtils.makedirs(path) unless File.exist?(path)
174:         end
file_path_key(path) click to toggle source

Translate a file path into a key.

     # File lib/active_support/cache/file_store.rb, line 157
157:         def file_path_key(path)
158:           fname = path[cache_path.size, path.size].split(File::SEPARATOR, 4).last
159:           fname.gsub(UNESCAPE_FILENAME_CHARS){|match| $1.ord.to_s(16)}
160:         end
key_file_path(key) click to toggle source

Translate a key into a file path.

     # File lib/active_support/cache/file_store.rb, line 138
138:         def key_file_path(key)
139:           fname = key.to_s.gsub(ESCAPE_FILENAME_CHARS){|match| "%#{match.ord.to_s(16).upcase}"}
140:           hash = Zlib.adler32(fname)
141:           hash, dir_1 = hash.divmod(0x1000)
142:           dir_2 = hash.modulo(0x1000)
143:           fname_paths = []
144:           # Make sure file name is < 255 characters so it doesn't exceed file system limits.
145:           if fname.size <= 255
146:             fname_paths << fname
147:           else
148:             while fname.size <= 255
149:               fname_path << fname[0, 255]
150:               fname = fname[255, 1]
151:             end
152:           end
153:           File.join(cache_path, DIR_FORMATTER % dir_1, DIR_FORMATTER % dir_2, *fname_paths)
154:         end
search_dir(dir, &callback) click to toggle source
     # File lib/active_support/cache/file_store.rb, line 176
176:         def search_dir(dir, &callback)
177:           Dir.foreach(dir) do |d|
178:             next if d == "." || d == ".."
179:             name = File.join(dir, d)
180:             if File.directory?(name)
181:               search_dir(name, &callback)
182:             else
183:               callback.call name
184:             end
185:           end
186:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.