class Roda::RodaCache

  1. lib/roda/cache.rb
Superclass: Object

A thread safe cache class, offering only [] and []= methods, each protected by a mutex.

Methods

Public Class

  1. new

Public Instance

  1. []
  2. []=
  3. freeze

Public Class methods

new()

Create a new thread safe cache.

[show source]
   # File lib/roda/cache.rb
10 def initialize
11   @mutex = Mutex.new
12   @hash = {}
13 end

Public Instance methods

[](key)

Make getting value from underlying hash thread safe.

[show source]
   # File lib/roda/cache.rb
16 def [](key)
17   @mutex.synchronize{@hash[key]}
18 end
[]=(key, value)

Make setting value in underlying hash thread safe.

[show source]
   # File lib/roda/cache.rb
21 def []=(key, value)
22   @mutex.synchronize{@hash[key] = value}
23 end
freeze()

Return the frozen internal hash. The internal hash can then be accessed directly since it is frozen and there are no thread safety issues.

[show source]
   # File lib/roda/cache.rb
28 def freeze
29   @hash.freeze
30 end