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.
# File lib/active_support/cache/file_store.rb, line 27
27: def cleanup(options = nil)
28: options = merged_options(options)
29: each_key(options) do |key|
30: entry = read_entry(key, options)
31: delete_entry(key, options) if entry && entry.expired?
32: end
33: end
# File lib/active_support/cache/file_store.rb, line 22
22: def clear(options = nil)
23: root_dirs = Dir.entries(cache_path).reject{|f| ['.', '..'].include?(f)}
24: FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)})
25: end
# File lib/active_support/cache/file_store.rb, line 49
49: def decrement(name, amount = 1, options = nil)
50: file_name = key_file_path(namespaced_key(name, options))
51: lock_file(file_name) do
52: options = merged_options(options)
53: if num = read(name, options)
54: num = num.to_i - amount
55: write(name, num, options)
56: num
57: else
58: nil
59: end
60: end
61: end
# File lib/active_support/cache/file_store.rb, line 63
63: def delete_matched(matcher, options = nil)
64: options = merged_options(options)
65: instrument(:delete_matched, matcher.inspect) do
66: matcher = key_matcher(matcher, options)
67: search_dir(cache_path) do |path|
68: key = file_path_key(path)
69: delete_entry(key, options) if key.match(matcher)
70: end
71: end
72: end
# File lib/active_support/cache/file_store.rb, line 35
35: def increment(name, amount = 1, options = nil)
36: file_name = key_file_path(namespaced_key(name, options))
37: lock_file(file_name) do
38: options = merged_options(options)
39: if num = read(name, options)
40: num = num.to_i + amount
41: write(name, num, options)
42: num
43: else
44: nil
45: end
46: end
47: end
# File lib/active_support/cache/file_store.rb, line 104
104: def delete_entry(key, options)
105: file_name = key_file_path(key)
106: if File.exist?(file_name)
107: begin
108: File.delete(file_name)
109: delete_empty_directories(File.dirname(file_name))
110: true
111: rescue => e
112: # Just in case the error was caused by another process deleting the file first.
113: raise e if File.exist?(file_name)
114: false
115: end
116: end
117: end
# File lib/active_support/cache/file_store.rb, line 76
76: def read_entry(key, options)
77: file_name = key_file_path(key)
78: if File.exist?(file_name)
79: entry = File.open(file_name) { |f| Marshal.load(f) }
80: if entry && !entry.expired? && !entry.expires_in && !self.options[:expires_in]
81: # Check for deprecated use of +:expires_in+ option from versions < 3.0
82: deprecated_expires_in = options[:expires_in]
83: if deprecated_expires_in
84: ActiveSupport::Deprecation.warn('Setting :expires_in on read has been deprecated in favor of setting it on write.', caller)
85: if entry.created_at + deprecated_expires_in.to_f <= Time.now.to_f
86: delete_entry(key, options)
87: entry = nil
88: end
89: end
90: end
91: entry
92: end
93: rescue
94: nil
95: end
# File lib/active_support/cache/file_store.rb, line 97
97: def write_entry(key, entry, options)
98: file_name = key_file_path(key)
99: ensure_cache_path(File.dirname(file_name))
100: File.atomic_write(file_name, cache_path) {|f| Marshal.dump(entry, f)}
101: true
102: end
Delete empty directories in the cache.
# File lib/active_support/cache/file_store.rb, line 162
162: def delete_empty_directories(dir)
163: return if dir == cache_path
164: if Dir.entries(dir).reject{|f| ['.', '..'].include?(f)}.empty?
165: File.delete(dir) rescue nil
166: delete_empty_directories(File.dirname(dir))
167: end
168: end
Make sure a file path’s directories exist.
# File lib/active_support/cache/file_store.rb, line 171
171: def ensure_cache_path(path)
172: FileUtils.makedirs(path) unless File.exist?(path)
173: end
Translate a file path into a key.
# File lib/active_support/cache/file_store.rb, line 156
156: def file_path_key(path)
157: fname = path[cache_path.size, path.size].split(File::SEPARATOR, 4).last
158: Rack::Utils.unescape(fname)
159: end
Translate a key into a file path.
# File lib/active_support/cache/file_store.rb, line 137
137: def key_file_path(key)
138: fname = Rack::Utils.escape(key)
139: hash = Zlib.adler32(fname)
140: hash, dir_1 = hash.divmod(0x1000)
141: dir_2 = hash.modulo(0x1000)
142: fname_paths = []
143: # Make sure file name is < 255 characters so it doesn't exceed file system limits.
144: if fname.size <= 255
145: fname_paths << fname
146: else
147: while fname.size <= 255
148: fname_path << fname[0, 255]
149: fname = fname[255, 1]
150: end
151: end
152: File.join(cache_path, DIR_FORMATTER % dir_1, DIR_FORMATTER % dir_2, *fname_paths)
153: end
# File lib/active_support/cache/file_store.rb, line 175
175: def search_dir(dir, &callback)
176: Dir.foreach(dir) do |d|
177: next if d == "." || d == ".."
178: name = File.join(dir, d)
179: if File.directory?(name)
180: search_dir(name, &callback)
181: else
182: callback.call name
183: end
184: end
185: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.