1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 56 57 58 59 60 61 62 63 64
|
require 'singleton'
require 'monitor'
require 'thread_safe'
module RablRails
class Library
include Singleton
def initialize
@cached_templates = ThreadSafe::Cache.new
@mutex = Monitor.new
end
def reset_cache!
@cached_templates = ThreadSafe::Cache.new
end
def get_rendered_template(source, view, locals = nil)
compiled_template = compile_template_from_source(source, view)
format = view.params[:format] ? view.params[:format].to_s.upcase : :JSON
Renderers.const_get(format).render(compiled_template, view, locals)
end
def compile_template_from_source(source, view)
if RablRails.configuration.cache_templates
path = view.instance_variable_get(:@virtual_path)
synchronized_compile(path, source, view)
else
compile(source, view)
end
end
def compile_template_from_path(path, view)
if RablRails.configuration.cache_templates
synchronized_compile(path, nil, view)
else
source = fetch_source(path, view)
compile(source, view)
end
end
private
def synchronized_compile(path, source, view)
@cached_templates[path] || @mutex.synchronize do
# Any thread holding this lock will be compiling the template needed
# by the threads waiting. So re-check the template presence to avoid
# re-compilation
@cached_templates.fetch(path) do
source ||= fetch_source(path, view)
@cached_templates[path] = compile(source, view)
end
end
end
def compile(source, view)
Compiler.new(view).compile_source(source)
end
def fetch_source(path, view)
view.lookup_context.find_template(path, [], false).source
end
end
end
|