Class: Higgs::SharedWorkCache

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

Overview

multi-thread safe cache

Constant Summary collapse

CVS_ID =

for ident(1)

'$Id: cache.rb 841 2008-12-24 09:23:20Z toki $'

Instance Method Summary collapse

Constructor Details

#initialize(cache = {}, &work) ⇒ SharedWorkCache



78
79
80
81
82
83
84
85
# File 'lib/higgs/cache.rb', line 78

def initialize(cache={}, &work)
  unless (work) then
    raise ArgumentError, 'required work block'
  end
  @work = work
  @lock = Mutex.new
  @cache = cache
end

Instance Method Details

#[](key) ⇒ Object



98
99
100
# File 'lib/higgs/cache.rb', line 98

def [](key)
  fetch_work(key).result
end

#[]=(key, value) ⇒ Object



102
103
104
105
# File 'lib/higgs/cache.rb', line 102

def []=(key, value)
  work = fetch_work(key)
  work.result = value
end

#delete(key) ⇒ Object



107
108
109
110
# File 'lib/higgs/cache.rb', line 107

def delete(key)
  r = @lock.synchronize{ @cache.delete(key) }
  r ? true : false
end