Module: TCCache

Defined in:
lib/rbbt/util/tc_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(file, type = :single) ⇒ Object



4
5
6
7
# File 'lib/rbbt/util/tc_cache.rb', line 4

def self.open(file, type = :single)
  database = Persist.open_tokyocabinet(file, true, type, "HDB")
  database.extend TCCache
end

Instance Method Details

#cache(key) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbbt/util/tc_cache.rb', line 9

def cache(key)

  self.read_and_close do
    return self[key] if self.include? key
  end

  value = yield

  self.write_and_close do
    self[key] = value
  end

  value
end