Class: Roda::RodaCache
- Inherits:
-
Object
- Object
- Roda::RodaCache
- Defined in:
- lib/roda/cache.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.
-
#freeze ⇒ Object
Return the frozen internal hash.
-
#initialize ⇒ RodaCache
constructor
Create a new thread safe cache.
Constructor Details
#initialize ⇒ RodaCache
Create a new thread safe cache.
10 11 12 13 |
# File 'lib/roda/cache.rb', line 10 def initialize @mutex = Mutex.new @hash = {} end |
Instance Method Details
#[](key) ⇒ Object
Make getting value from underlying hash thread safe.
16 17 18 |
# File 'lib/roda/cache.rb', line 16 def [](key) @mutex.synchronize{@hash[key]} end |
#[]=(key, value) ⇒ Object
Make setting value in underlying hash thread safe.
21 22 23 |
# File 'lib/roda/cache.rb', line 21 def []=(key, value) @mutex.synchronize{@hash[key] = value} end |
#freeze ⇒ Object
Return the frozen internal hash. The internal hash can then be accessed directly since it is frozen and there are no thread safety issues.
28 29 30 |
# File 'lib/roda/cache.rb', line 28 def freeze @hash.freeze end |