Skip to content

Commit

Permalink
make template cache thread-safe: metanorma/iso-10303#380
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Oct 20, 2024
1 parent e6b0fb8 commit 32cb88c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/relaton/render/template/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ module Template
class CacheManager
include Singleton

attr_accessor :mutex

def initialize
@cache = {}
@mutex = Mutex.new
end

def store(key, value)
Expand Down Expand Up @@ -74,9 +77,14 @@ def punct_field?(name)

def template_process(template)
template.is_a?(String) or return template
t = @templatecache.retrieve(template) and return t
t = ::Liquid::Template.parse(add_field_delim_to_template(template))
@templatecache.store(template, t)
t = nil
@templatecache.mutex.synchronize do
unless t = @templatecache.retrieve(template)
t = ::Liquid::Template
.parse(add_field_delim_to_template(template))
@templatecache.store(template, t)
end
end
t
end

Expand Down

0 comments on commit 32cb88c

Please sign in to comment.