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(&block) ⇒ Object
- #each_with_object(memo, &block) ⇒ Object
-
#initialize ⇒ ComponentCache
constructor
Create a new thread safe cache.
- #inject(memo, &block) ⇒ Object
- #key?(key) ⇒ Boolean
- #map(&block) ⇒ Object
Constructor Details
#initialize ⇒ ComponentCache
Create a new thread safe cache.
16 17 18 19 |
# File 'lib/ditty.rb', line 16 def initialize @mutex = Mutex.new @hash = {} end |
Instance Method Details
#[](key) ⇒ Object
Make getting value from underlying hash thread safe.
22 23 24 |
# File 'lib/ditty.rb', line 22 def [](key) @mutex.synchronize { @hash[key] } end |
#[]=(key, value) ⇒ Object
Make setting value in underlying hash thread safe.
27 28 29 |
# File 'lib/ditty.rb', line 27 def []=(key, value) @mutex.synchronize { @hash[key] = value } end |
#each(&block) ⇒ Object
35 36 37 |
# File 'lib/ditty.rb', line 35 def each(&block) @mutex.synchronize { @hash.each(&block) } end |
#each_with_object(memo, &block) ⇒ Object
43 44 45 |
# File 'lib/ditty.rb', line 43 def each_with_object(memo, &block) @mutex.synchronize { @hash.each_with_object(memo, &block) } end |
#inject(memo, &block) ⇒ Object
39 40 41 |
# File 'lib/ditty.rb', line 39 def inject(memo, &block) @mutex.synchronize { @hash.inject(memo, &block) } end |
#key?(key) ⇒ Boolean
47 48 49 |
# File 'lib/ditty.rb', line 47 def key?(key) @hash.key? key end |
#map(&block) ⇒ Object
31 32 33 |
# File 'lib/ditty.rb', line 31 def map(&block) @mutex.synchronize { @hash.map(&block) } end |