Class: RedParse::Cache
- Inherits:
-
Object
- Object
- RedParse::Cache
- Defined in:
- lib/redparse/cache.rb
Instance Method Summary collapse
- #all_entry_files ⇒ Object
- #cachedir ⇒ Object
- #get(input) ⇒ Object
- #hash_of_input(input) ⇒ Object
-
#initialize(*params) ⇒ Cache
constructor
A new instance of Cache.
- #put(input, result) ⇒ Object
Constructor Details
#initialize(*params) ⇒ Cache
Returns a new instance of Cache.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/redparse/cache.rb', line 4 def initialize *params @callersfile=Digest::SHA2.hexdigest params.join(',') @homedir=find_home+"/.redparse/" Dir.mkdir @homedir unless File.exist? @homedir Dir.mkdir cachedir unless File.exist? cachedir saved_digest= File.open(@homedir+"/parserdigest","rb"){|fd| fd.read.chomp } if File.exist?(@homedir+"/parserdigest") actual_digest= @@saved_parser_digest ||= redparse_rb_hexdigest if saved_digest!=actual_digest File.unlink(*all_entry_files) #flush cache File.open(@homedir+"/parserdigest","wb"){|fd| fd.puts actual_digest } #update saved digest end retire_old_entries end |
Instance Method Details
#all_entry_files ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/redparse/cache.rb', line 26 def all_entry_files Dir[@homedir+"*"].select{|fn| File.directory? fn }.map{|dirname| Dir[dirname+"/*"] }.flatten end |
#cachedir ⇒ Object
18 19 20 |
# File 'lib/redparse/cache.rb', line 18 def cachedir @homedir+@callersfile+"/" end |
#get(input) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/redparse/cache.rb', line 108 def get input hash=hash_of_input input cachefile=cachedir+hash if File.exist? cachefile result=File.open(cachefile,"rb"){|fd| line=fd.readline fd.rewind if /#encoded with Ron\n/i===line begin require 'ron' Ron.load fd.read rescue Exception return nil end else begin Marshal.load fd rescue Exception=>e warn "#{e.class}: #{e}" warn "cache read failed for:\n#{input}" return nil end end } begin t=Time.now File.utime(t,t,cachefile) rescue Exception File.open(cachefile,"a"){|fd| } #touch cache date end return result end rescue EOFError return nil end |
#hash_of_input(input) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/redparse/cache.rb', line 100 def hash_of_input input if IO===input hexdigest_of_file input else Digest::SHA2.hexdigest input end end |
#put(input, result) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/redparse/cache.rb', line 145 def put input,result hash=hash_of_input input File.open(cachedir+hash, "wb"){|fd| begin Thread.current["Marshal.ignore_sclass"]=true Marshal.dump(result,fd) rescue TypeError=>e #dump failed File.unlink cachedir+hash begin require 'ron' File.open(cachedir+hash, "wb"){|fd2| fd2.write "#encoded with Ron\n" fd2.write Ron.dump(result) } rescue Exception return end ensure Thread.current["Marshal.ignore_sclass"]=nil end } rescue Exception=>e #dump failed warn "#{e.class}: #{e}" warn "cache write failed for:\n#{result.inspect}" File.unlink cachedir+hash end |