Module: FileCache
- Defined in:
- lib/rbbt/util/filecache.rb
Overview
Provides caching functionality for files downloaded from the internet
Constant Summary collapse
- CACHEDIR =
"/tmp/rbbt_cache"
Class Method Summary collapse
- .add(filename, content) ⇒ Object
- .cache_online_elements(ids, pattern = nil, &block) ⇒ Object
- .cachedir ⇒ Object
- .cachedir=(cachedir) ⇒ Object
- .del(filename) ⇒ Object
- .found(filename) ⇒ Object
- .get(filename) ⇒ Object
- .path(filename) ⇒ Object
Class Method Details
.add(filename, content) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rbbt/util/filecache.rb', line 33 def self.add(filename, content) path = path(filename) FileUtils.makedirs(File.dirname(path), :mode => 0777) Misc.sensiblewrite(path, content) FileUtils.chmod 0666, path path end |
.cache_online_elements(ids, pattern = nil, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rbbt/util/filecache.rb', line 63 def self.cache_online_elements(ids, pattern = nil, &block) ids = [ids] unless Array === ids result_files = {} missing = [] ids.each do |id| filename = pattern ? pattern.sub("{ID}", id.to_s) : id.to_s if FileCache.found(filename) result_files[id] = FileCache.path(filename) else missing << id end end yield(missing).each do |id, content| filename = pattern ? pattern.sub("{ID}", id.to_s) : id.to_s path = FileCache.path(filename) Open.write(path, content) result_files[id] = path end if missing.any? result_files end |
.cachedir ⇒ Object
18 19 20 |
# File 'lib/rbbt/util/filecache.rb', line 18 def self.cachedir CACHEDIR end |
.cachedir=(cachedir) ⇒ Object
13 14 15 16 |
# File 'lib/rbbt/util/filecache.rb', line 13 def self.cachedir=(cachedir) CACHEDIR.replace cachedir FileUtils.mkdir_p CACHEDIR unless File.exist? CACHEDIR end |
.del(filename) ⇒ Object
57 58 59 60 61 |
# File 'lib/rbbt/util/filecache.rb', line 57 def self.del(filename) path = path(filename) FileUtils.rm path if File.exist? path end |
.found(filename) ⇒ Object
45 46 47 |
# File 'lib/rbbt/util/filecache.rb', line 45 def self.found(filename) File.exist? FileCache.path(filename) end |
.get(filename) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rbbt/util/filecache.rb', line 49 def self.get(filename) path = path(filename) return nil if ! File.exist? path File.open(path) end |
.path(filename) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rbbt/util/filecache.rb', line 22 def self.path(filename) filename = File.basename filename filename.match(/(.+)\.(.+)/) base = filename.sub(/\..+/,'') dirs = base.scan(/./).reverse.values_at(0,1,2,3,4).compact File.join(File.join(CACHEDIR, *dirs), filename) end |