Class: Roda::RodaCache

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeRodaCache

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

#freezeObject

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