Class: Spider::TemplateCache

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/spiderfw/cache/template_cache.rb

Instance Method Summary collapse

Methods included from Logger

add, check_request_level, close, close_all, datetime_format, datetime_format=, #debug, debug, debug?, #debug?, enquire_loggers, #error, error, #error?, error?, fatal, #fatal, fatal?, #fatal?, info, #info, info?, #info?, #log, log, method_missing, open, reopen, request_level, send_to_loggers, set_request_level, unknown, #unknown, warn, #warn, warn?, #warn?

Constructor Details

#initialize(root_path) ⇒ TemplateCache



10
11
12
13
14
# File 'lib/spiderfw/cache/template_cache.rb', line 10

def initialize(root_path)
    FileUtils.mkpath(root_path)
    @path = root_path
    @invalid = {}
end

Instance Method Details

#clear!Object



139
140
141
# File 'lib/spiderfw/cache/template_cache.rb', line 139

def clear!
    FileUtils.rm_rf(Dir.glob(File.join(@path, '*')))
end

#fetch(path, &block) ⇒ Object



16
17
18
19
# File 'lib/spiderfw/cache/template_cache.rb', line 16

def fetch(path, &block)
    return refresh(path, &block) unless fresh?(path)
    return load_cache(path)
end

#fresh?(path) ⇒ Boolean



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spiderfw/cache/template_cache.rb', line 26

def fresh?(path)
    full_path = get_location(path)
    if Spider.config.get('template.cache.disable')
        Spider::Request.current[:compiled_templates] ||= {}
        return true if Spider::Request.current[:compiled_templates][full_path]
        #debug("Cache disabled, recreating #{full_path}")
        return false
    end
    exists = File.exist?(full_path)
    return true if Spider.config.get('template.cache.no_check') && exists
    return false if @invalid[path]
    global_reload_file = File.join(Spider.paths[:tmp], 'templates_reload.txt')
    check_file = File.join(full_path, 'check')
    return false unless File.exist?(check_file)
    if File.exist?(global_reload_file)
        return false if (File.mtime(global_reload_file) > File.mtime(check))
    end
    return true unless Spider.conf.get('template.cache.check_files')
    lock_file = File.new(File.join(full_path, 'lock'))
    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.binread(check_file)).each do |check, time|
        #debug("Template file #{check} changed, refreshing cache")
        return false if File.mtime(check) > time
    end
    lock_file.flock(File::LOCK_UN)
    return true
end

#get_compiled_template(path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/spiderfw/cache/template_cache.rb', line 68

def get_compiled_template(path)
    compiled = Spider::CompiledTemplate.new
    compiled.cache_path = path
    init_code = IO.read(File.join(path, 'init.rb'))
    run_code = IO.read(File.join(path, 'run.rb'))
    compiled.assets = Marshal.load(IO.binread(File.join(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 = File.join(path, entry)
        next if entry == '__info'
        next unless File.directory?(sub_path)
        compiled.subtemplates[entry] = get_compiled_template(sub_path)
    end
    return compiled
end

#get_location(path, &block) ⇒ Object



21
22
23
24
# File 'lib/spiderfw/cache/template_cache.rb', line 21

def get_location(path, &block)
    refresh(path, &block) if (block && !fresh?(path))
    return File.join(@path, path)
end

#invalidate(path) ⇒ Object



57
58
59
# File 'lib/spiderfw/cache/template_cache.rb', line 57

def invalidate(path)
    @invalid[path] = true
end

#load_cache(template_path) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/spiderfw/cache/template_cache.rb', line 86

def load_cache(template_path)
    # debug("Using cached #{template_path}")
    full_path = get_location(template_path)
    lock_file = File.new(File.join(full_path, 'lock'))
    lock_file.flock(File::LOCK_SH)
    compiled = get_compiled_template(full_path)
    lock_file.flock(File::LOCK_UN)
    return compiled
end

#refresh(path, &block) ⇒ Object



61
62
63
64
65
66
# File 'lib/spiderfw/cache/template_cache.rb', line 61

def refresh(path, &block)
    #debug("Refreshing cache for #{path}")
    res = block.call()
    write_cache(path, res)
    return res
end

#write_cache(template_path, compiled_template) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/spiderfw/cache/template_cache.rb', line 126

def write_cache(template_path, compiled_template)
    full_path = get_location(template_path)
    FileUtils.mkpath(full_path)
    lock_file = File.new(File.join(full_path, 'lock'), 'w')
    lock_file.flock(File::LOCK_EX)
    write_compiled_template(compiled_template, full_path)
    modified = compiled_template.collect_mtimes
    File.open(File.join(full_path, 'check'), 'wb') do |file|
        file.puts(Marshal.dump(modified))
    end
    lock_file.flock(File::LOCK_UN)
end

#write_compiled_template(compiled, path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/spiderfw/cache/template_cache.rb', line 96

def write_compiled_template(compiled, path)
    compiled.cache_path = path
    File.open(File.join(path, 'init.rb'), 'w') do |file|
        file.puts(compiled.block.init_code)
    end
    File.open(File.join(path, 'run.rb'), 'w') do |file|
        file.puts(compiled.block.run_code)
    end
    File.open(File.join(path, 'assets'), 'wb') do |file|
        file.puts(Marshal.dump(compiled.assets))
    end
    compiled.subtemplates.each do |id, sub|
        sub_path = File.join(path, id)
        FileUtils.mkpath(sub_path)
        write_compiled_template(sub, sub_path)
    end
    compiled.devel_info.each do |name, val|
        sub_path = File.join(path, '__info')
        FileUtils.mkpath(sub_path)
        sub_path = File.join(sub_path, 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