lib/zache.rb in zache-0.1.0 vs lib/zache.rb in zache-0.2.0

- old
+ new

@@ -25,25 +25,36 @@ # Cache. # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2018 Yegor Bugayenko # License:: MIT class Zache - def initialize + def initialize(sync: true) @hash = {} + @sync = sync @mutex = Mutex.new end def get(key, lifetime: 60 * 60) - @mutex.synchronize do - rec = @hash[key] - rec = nil if !rec.nil? && rec[:start] < Time.now - rec[:lifetime] - if rec.nil? - @hash[key] = { - value: yield, - start: Time.now, - lifetime: lifetime - } + if @sync + @mutex.synchronize do + calc(key, lifetime) { yield } end - @hash[key][:value] + else + calc(key, lifetime) { yield } end + end + + private + + def calc(key, lifetime) + rec = @hash[key] + rec = nil if !rec.nil? && rec[:start] < Time.now - rec[:lifetime] + if rec.nil? + @hash[key] = { + value: yield, + start: Time.now, + lifetime: lifetime + } + end + @hash[key][:value] end end