Class: Ditty::ComponentCache
- Inherits:
-
Object
- Object
- Ditty::ComponentCache
- Defined in:
- lib/ditty.rb
Overview
A thread safe cache class, offering only #[] and #[]= methods, each protected by a mutex. Ripped off from Roda - github.com/jeremyevans/roda
Instance Method Summary collapse
-
#[](key) ⇒ Object
Make getting value from underlying hash thread safe.
-
#[]=(key, value) ⇒ Object
Make setting value in underlying hash thread safe.
- #each_with_object(memo, &block) ⇒ Object
-
#initialize ⇒ ComponentCache
constructor
Create a new thread safe cache.
- #inject(memo, &block) ⇒ Object
- #map(&block) ⇒ Object
Constructor Details
#initialize ⇒ ComponentCache
Create a new thread safe cache.
14 15 16 17 |
# File 'lib/ditty.rb', line 14 def initialize @mutex = Mutex.new @hash = {} end |
Instance Method Details
#[](key) ⇒ Object
Make getting value from underlying hash thread safe.
20 21 22 |
# File 'lib/ditty.rb', line 20 def [](key) @mutex.synchronize { @hash[key] } end |
#[]=(key, value) ⇒ Object
Make setting value in underlying hash thread safe.
25 26 27 |
# File 'lib/ditty.rb', line 25 def []=(key, value) @mutex.synchronize { @hash[key] = value } end |
#each_with_object(memo, &block) ⇒ Object
37 38 39 |
# File 'lib/ditty.rb', line 37 def each_with_object(memo, &block) @mutex.synchronize { @hash.each_with_object(memo, &block) } end |
#inject(memo, &block) ⇒ Object
33 34 35 |
# File 'lib/ditty.rb', line 33 def inject(memo, &block) @mutex.synchronize { @hash.inject(memo, &block) } end |
#map(&block) ⇒ Object
29 30 31 |
# File 'lib/ditty.rb', line 29 def map(&block) @mutex.synchronize { @hash.map(&block) } end |