Class: Roda::RodaCache
- Inherits:
-
Object
- Object
- Roda::RodaCache
- Defined in:
- lib/roda.rb
Overview
A thread safe cache class, offering only #[] and #[]= methods, each protected by a mutex.
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 ⇒ RodaCache
constructor
Create a new thread safe cache.
Constructor Details
#initialize ⇒ RodaCache
Create a new thread safe cache.
16 17 18 19 |
# File 'lib/roda.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/roda.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/roda.rb', line 27 def []=(key, value) @mutex.synchronize{@hash[key] = value} end |