| Class | Cacher |
| In: |
lib/cacher.rb
lib/cacher.rb |
| Parent: | Object |
| cachedir | [RW] | |
| cachedir | [RW] |
# File lib/cacher.rb, line 6
6: def initialize(dirs)
7: self.cachedir = dirs.find { |d|
8: begin
9: d if (File.writable?(d) and File.directory?(d)) or FileUtils.mkdir_p(d)
10: rescue Errno::EACCES
11: false
12: end
13: }
14: if !cachedir
15: raise "Cannot create cache dir"
16: end
17: end
# File lib/cacher.rb, line 6
6: def initialize(dirs)
7: self.cachedir = dirs.find { |d|
8: begin
9: d if (File.writable?(d) and File.directory?(d)) or FileUtils.mkdir_p(d)
10: rescue Errno::EACCES
11: false
12: end
13: }
14: if !cachedir
15: raise "Cannot create cache dir"
16: end
17: end
# File lib/cacher.rb, line 18
18: def cached(file)
19: c = mangle(file)
20: if File.exist? c and File.stat(c).mtime >= File.stat(file).mtime
21: return File.read(c)
22: else
23: content = yield(file)
24: File.open(c, 'w') { |f| f.puts(content) }
25: return content
26: end
27: end
# File lib/cacher.rb, line 18
18: def cached(file)
19: c = mangle(file)
20: if File.exist? c and File.stat(c).mtime >= File.stat(file).mtime
21: return File.read(c)
22: else
23: content = yield(file)
24: File.open(c, 'w') { |f| f.puts(content) }
25: return content
26: end
27: end
# File lib/cacher.rb, line 28
28: def mangle(filename)
29: md5 = Digest::MD5.new(filename).to_s
30: File.join(cachedir, md5)
31: end