Class: Roda::RodaCache

Inherits:
Object
  • Object
show all
Defined in:
lib/roda.rb

Overview

A thread safe cache class, offering only #[] and #[]= methods, each protected by a mutex. Used on non-MRI where Hash is not thread safe.

Instance Method Summary collapse

Constructor Details

#initializeRodaCache

Create a new thread safe cache.



19
20
21
22
# File 'lib/roda.rb', line 19

def initialize
  @mutex = Mutex.new
  @hash = {}
end

Instance Method Details

#[](key) ⇒ Object

Make getting value from underlying hash thread safe.



25
26
27
# File 'lib/roda.rb', line 25

def [](key)
  @mutex.synchronize{@hash[key]}
end

#[]=(key, value) ⇒ Object

Make setting value in underlying hash thread safe.



30
31
32
# File 'lib/roda.rb', line 30

def []=(key, value)
  @mutex.synchronize{@hash[key] = value}
end