class Roda::RodaCache
A thread safe cache class, offering only []
and []=
methods, each protected by a mutex.
Public Class Methods
Source
# File lib/roda/cache.rb, line 10 def initialize @mutex = Mutex.new @hash = {} end
Create a new thread safe cache.
Public Instance Methods
Source
# File lib/roda/cache.rb, line 16 def [](key) @mutex.synchronize{@hash[key]} end
Make getting value from underlying hash thread safe.
Source
# File lib/roda/cache.rb, line 21 def []=(key, value) @mutex.synchronize{@hash[key] = value} end
Make setting value in underlying hash thread safe.
Source
# File lib/roda/cache.rb, line 28 def freeze @hash.freeze end
Return the frozen internal hash. The internal hash can then be accessed directly since it is frozen and there are no thread safety issues.
Private Instance Methods
Source
# File lib/roda/cache.rb, line 35 def initialize_copy(other) @mutex = Mutex.new other.instance_variable_get(:@mutex).synchronize do @hash = other.instance_variable_get(:@hash).dup end end
Create a copy of the cache with a separate mutex.