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 30 30: def cleanup(options = nil) 31: options = merged_options(options) 32: each_key(options) do |key| 33: entry = read_entry(key, options) 34: delete_entry(key, options) if entry && entry.expired? 35: end 36: end
# File lib/active_support/cache/file_store.rb, line 25 25: def clear(options = nil) 26: root_dirs = Dir.entries(cache_path).reject{|f| f.in?(EXCLUDED_DIRS)} 27: FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)}) 28: end
# File lib/active_support/cache/file_store.rb, line 52 52: def decrement(name, amount = 1, options = nil) 53: file_name = key_file_path(namespaced_key(name, options)) 54: lock_file(file_name) do 55: options = merged_options(options) 56: if num = read(name, options) 57: num = num.to_i - amount 58: write(name, num, options) 59: num 60: else 61: nil 62: end 63: end 64: end
# File lib/active_support/cache/file_store.rb, line 66 66: def delete_matched(matcher, options = nil) 67: options = merged_options(options) 68: instrument(:delete_matched, matcher.inspect) do 69: matcher = key_matcher(matcher, options) 70: search_dir(cache_path) do |path| 71: key = file_path_key(path) 72: delete_entry(key, options) if key.match(matcher) 73: end 74: end 75: end
# File lib/active_support/cache/file_store.rb, line 38 38: def increment(name, amount = 1, options = nil) 39: file_name = key_file_path(namespaced_key(name, options)) 40: lock_file(file_name) do 41: options = merged_options(options) 42: if num = read(name, options) 43: num = num.to_i + amount 44: write(name, num, options) 45: num 46: else 47: nil 48: end 49: end 50: end
# File lib/active_support/cache/file_store.rb, line 95 95: def delete_entry(key, options) 96: file_name = key_file_path(key) 97: if File.exist?(file_name) 98: begin 99: File.delete(file_name) 100: delete_empty_directories(File.dirname(file_name)) 101: true 102: rescue => e 103: # Just in case the error was caused by another process deleting the file first. 104: raise e if File.exist?(file_name) 105: false 106: end 107: end 108: end
# File lib/active_support/cache/file_store.rb, line 79 79: def read_entry(key, options) 80: file_name = key_file_path(key) 81: if File.exist?(file_name) 82: File.open(file_name) { |f| Marshal.load(f) } 83: end 84: rescue 85: nil 86: end
# File lib/active_support/cache/file_store.rb, line 88 88: def write_entry(key, entry, options) 89: file_name = key_file_path(key) 90: ensure_cache_path(File.dirname(file_name)) 91: File.atomic_write(file_name, cache_path) {|f| Marshal.dump(entry, f)} 92: true 93: end
Delete empty directories in the cache.
# File lib/active_support/cache/file_store.rb, line 151 151: def delete_empty_directories(dir) 152: return if dir == cache_path 153: if Dir.entries(dir).reject{|f| f.in?(EXCLUDED_DIRS)}.empty? 154: File.delete(dir) rescue nil 155: delete_empty_directories(File.dirname(dir)) 156: end 157: end
Make sure a file path’s directories exist.
# File lib/active_support/cache/file_store.rb, line 160 160: def ensure_cache_path(path) 161: FileUtils.makedirs(path) unless File.exist?(path) 162: end
Translate a file path into a key.
# File lib/active_support/cache/file_store.rb, line 145 145: def file_path_key(path) 146: fname = path[cache_path.size, path.size].split(File::SEPARATOR, 4).last 147: Rack::Utils.unescape(fname) 148: end
Translate a key into a file path.
# File lib/active_support/cache/file_store.rb, line 128 128: def key_file_path(key) 129: fname = Rack::Utils.escape(key) 130: hash = Zlib.adler32(fname) 131: hash, dir_1 = hash.divmod(0x1000) 132: dir_2 = hash.modulo(0x1000) 133: fname_paths = [] 134: 135: # Make sure file name doesn't exceed file system limits. 136: begin 137: fname_paths << fname[0, FILENAME_MAX_SIZE] 138: fname = fname[FILENAME_MAX_SIZE..1] 139: end until fname.blank? 140: 141: File.join(cache_path, DIR_FORMATTER % dir_1, DIR_FORMATTER % dir_2, *fname_paths) 142: end
# File lib/active_support/cache/file_store.rb, line 164 164: def search_dir(dir, &callback) 165: return if !File.exist?(dir) 166: Dir.foreach(dir) do |d| 167: next if d.in?(EXCLUDED_DIRS) 168: name = File.join(dir, d) 169: if File.directory?(name) 170: search_dir(name, &callback) 171: else 172: callback.call name 173: end 174: end 175: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.