The TTL store is a data structure which keeps data by a key, and with a time-to-live. It is specifically designed for data which is static, i.e. for a certain key in a sufficiently large span, the value will be the same.
Because of that, synchronizations around reads do not exist, while write synchronizations will be short-circuited by a read.
Constants
DEFAULT_TTL | = | 60 * 60 * 24 |
Public Class methods
new()
[show source]
# File lib/rodauth/oauth/ttl_store.rb 14 def initialize 15 @store_mutex = Mutex.new 16 @store = {} 17 end
Public Instance methods
[](key)
[show source]
# File lib/rodauth/oauth/ttl_store.rb 19 def [](key) 20 lookup(key, now) 21 end
set(key, &block)
[show source]
# File lib/rodauth/oauth/ttl_store.rb 23 def set(key, &block) 24 @store_mutex.synchronize do 25 # short circuit 26 return @store[key][:payload] if @store[key] && @store[key][:ttl] < now 27 end 28 29 payload, ttl = block.call 30 31 return payload unless ttl 32 33 @store_mutex.synchronize do 34 # given that the block call triggers network, and two requests for the same key be processed 35 # at the same time, this ensures the first one wins. 36 return @store[key][:payload] if @store[key] && @store[key][:ttl] < now 37 38 @store[key] = { payload: payload, ttl: ttl || (now + DEFAULT_TTL) } 39 end 40 @store[key][:payload] 41 end
uncache(key)
[show source]
# File lib/rodauth/oauth/ttl_store.rb 43 def uncache(key) 44 @store_mutex.synchronize do 45 @store.delete(key) 46 end 47 end