| Class | Rack::Cache::EntityStore::Disk |
| In: |
lib/rack/cache/entitystore.rb
|
| Parent: | EntityStore |
Stores entity bodies on disk at the specified path.
| root | [R] | Path where entities should be stored. This directory is created the first time the store is instansiated if it does not already exist. |
# File lib/rack/cache/entitystore.rb, line 90
90: def initialize(root)
91: @root = root
92: FileUtils.mkdir_p root, :mode => 0755
93: end
# File lib/rack/cache/entitystore.rb, line 162
162: def self.resolve(uri)
163: path = File.expand_path(uri.opaque || uri.path)
164: new path
165: end
# File lib/rack/cache/entitystore.rb, line 95
95: def exist?(key)
96: File.exist?(body_path(key))
97: end
# File lib/rack/cache/entitystore.rb, line 140
140: def purge(key)
141: File.unlink body_path(key)
142: nil
143: rescue Errno::ENOENT
144: nil
145: end
# File lib/rack/cache/entitystore.rb, line 99
99: def read(key)
100: File.open(body_path(key), 'rb') { |f| f.read }
101: rescue Errno::ENOENT
102: nil
103: end
# File lib/rack/cache/entitystore.rb, line 122
122: def write(body, ttl=nil)
123: filename = ['buf', $$, Thread.current.object_id].join('-')
124: temp_file = storage_path(filename)
125: key, size =
126: File.open(temp_file, 'wb') { |dest|
127: slurp(body) { |part| dest.write(part) }
128: }
129:
130: path = body_path(key)
131: if File.exist?(path)
132: File.unlink temp_file
133: else
134: FileUtils.mkdir_p File.dirname(path), :mode => 0755
135: FileUtils.mv temp_file, path
136: end
137: [key, size]
138: end
# File lib/rack/cache/entitystore.rb, line 158
158: def body_path(key)
159: storage_path spread(key)
160: end
# File lib/rack/cache/entitystore.rb, line 152
152: def spread(key)
153: key = key.dup
154: key[2,0] = '/'
155: key
156: end