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.
-
#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.
13 14 15 16 |
# File 'lib/ditty.rb', line 13 def initialize @mutex = Mutex.new @hash = {} end |
Instance Method Details
#[](key) ⇒ Object
Make getting value from underlying hash thread safe.
19 20 21 |
# File 'lib/ditty.rb', line 19 def [](key) @mutex.synchronize { @hash[key] } end |
#[]=(key, value) ⇒ Object
Make setting value in underlying hash thread safe.
24 25 26 |
# File 'lib/ditty.rb', line 24 def []=(key, value) @mutex.synchronize { @hash[key] = value } end |
#inject(memo, &block) ⇒ Object
32 33 34 |
# File 'lib/ditty.rb', line 32 def inject(memo, &block) @mutex.synchronize { @hash.inject(memo, &block) } end |
#map(&block) ⇒ Object
28 29 30 |
# File 'lib/ditty.rb', line 28 def map(&block) @mutex.synchronize { @hash.map(&block) } end |