class Ditty::ComponentCache
A thread safe cache class, offering only []
and []=
methods, each protected by a mutex. Ripped off from Roda - github.com/jeremyevans/roda
Public Class Methods
new()
click to toggle source
Create a new thread safe cache.
# File lib/ditty.rb, line 16 def initialize @mutex = Mutex.new @hash = {} end
Public Instance Methods
[](key)
click to toggle source
Make getting value from underlying hash thread safe.
# File lib/ditty.rb, line 22 def [](key) @mutex.synchronize { @hash[key] } end
[]=(key, value)
click to toggle source
Make setting value in underlying hash thread safe.
# File lib/ditty.rb, line 27 def []=(key, value) @mutex.synchronize { @hash[key] = value } end
each(&block)
click to toggle source
# File lib/ditty.rb, line 35 def each(&block) @mutex.synchronize { @hash.each(&block) } end
each_with_object(memo, &block)
click to toggle source
# File lib/ditty.rb, line 43 def each_with_object(memo, &block) @mutex.synchronize { @hash.each_with_object(memo, &block) } end
inject(memo, &block)
click to toggle source
# File lib/ditty.rb, line 39 def inject(memo, &block) @mutex.synchronize { @hash.inject(memo, &block) } end
key?(key)
click to toggle source
# File lib/ditty.rb, line 47 def key?(key) @hash.key? key end
map(&block)
click to toggle source
# File lib/ditty.rb, line 31 def map(&block) @mutex.synchronize { @hash.map(&block) } end