lib/spiderfw/cache/template_cache.rb in spiderfw-0.5.13 vs lib/spiderfw/cache/template_cache.rb in spiderfw-0.5.14
- old
+ new
@@ -22,15 +22,17 @@
refresh(path, &block) if (block && !fresh?(path))
return @path+'/'+path
end
def fresh?(path)
+ full_path = get_location(path)
if Spider.config.get('template.cache.disable')
- debug("Cache disabled, recreating #{path}")
+ Spider::Request.current[:compiled_templates] ||= {}
+ return true if Spider::Request.current[:compiled_templates][full_path]
+ #debug("Cache disabled, recreating #{full_path}")
return false
end
- full_path = get_location(path)
exists = File.exist?(full_path)
if (Spider.config.get('template.cache.no_check') && exists)
return true
end
return false if @invalid[path]
@@ -45,11 +47,11 @@
lock_file.flock(File::LOCK_SH)
File.new(full_path).flock(File::LOCK_SH)
# TODO: maybe insert here an (optional) tamper check
# that looks if the cache mtime is later then the saved time
Marshal.load(IO.read(check_file)).each do |check, time|
- debug("Template file #{check} changed, refreshing cache")
+ #debug("Template file #{check} changed, refreshing cache")
return false if File.mtime(check) > time
end
lock_file.flock(File::LOCK_UN)
return true
end
@@ -57,21 +59,22 @@
def invalidate(path)
@invalid[path] = true
end
def refresh(path, &block)
- debug("Refreshing cache for #{path}")
+ #debug("Refreshing cache for #{path}")
res = block.call()
write_cache(path, res)
return res
end
def get_compiled_template(path)
compiled = Spider::CompiledTemplate.new
compiled.cache_path = path
init_code = IO.read(path+'/init.rb')
run_code = IO.read(path+'/run.rb')
+ compiled.assets = Marshal.load(IO.read(path+'/assets'))
block = Spider::TemplateBlocks::CompiledBlock.new(init_code, run_code)
compiled.block = block
Dir.new(path).each do |entry|
next if entry[0].chr == '.'
sub_path = "#{path}/#{entry}"
@@ -81,11 +84,11 @@
end
return compiled
end
def load_cache(template_path)
- debug("Using cached #{template_path}")
+ # debug("Using cached #{template_path}")
full_path = get_location(template_path)
lock_file = File.new(full_path)
lock_file.flock(File::LOCK_SH)
compiled = get_compiled_template(full_path)
lock_file.flock(File::LOCK_UN)
@@ -98,10 +101,13 @@
file.puts(compiled.block.init_code)
end
File.open(path+'/run.rb', 'w') do |file|
file.puts(compiled.block.run_code)
end
+ File.open(path+'/assets', 'w') do |file|
+ file.puts(Marshal.dump(compiled.assets))
+ end
compiled.subtemplates.each do |id, sub|
sub_path = "#{path}/#{id}"
FileUtils.mkpath(sub_path)
write_compiled_template(sub, sub_path)
end
@@ -109,9 +115,13 @@
FileUtils.mkpath("#{path}/__info")
sub_path = "#{path}/__info/#{name}"
File.open(sub_path, 'w') do |f|
f.puts(val)
end
+ end
+ if Spider.config.get('template.cache.disable')
+ Spider::Request.current[:compiled_templates] ||= {}
+ Spider::Request.current[:compiled_templates][path] = true
end
end
def write_cache(template_path, compiled_template)
full_path = get_location(template_path)