lib/rodauth/oauth/ttl_store.rb in rodauth-oauth-0.8.0 vs lib/rodauth/oauth/ttl_store.rb in rodauth-oauth-0.9.0

- old
+ new

@@ -22,15 +22,21 @@ def set(key, &block) @store_mutex.synchronize do # short circuit return @store[key][:payload] if @store[key] && @store[key][:ttl] < now + end - payload, ttl = block.call - @store[key] = { payload: payload, ttl: (ttl || (now + DEFAULT_TTL)) } + payload, ttl = block.call - @store[key][:payload] + @store_mutex.synchronize do + # given that the block call triggers network, and two requests for the same key be processed + # at the same time, this ensures the first one wins. + return @store[key][:payload] if @store[key] && @store[key][:ttl] < now + + @store[key] = { payload: payload, ttl: (ttl || (now + DEFAULT_TTL)) } end + @store[key][:payload] end def uncache(key) @store_mutex.synchronize do @store.delete(key)