cache_manager.rb

Path: lib/cache_manager.rb
Last Update: Sun Apr 06 14:08:37 +0900 2008

$Id: cache_manager.rb 351 2008-04-06 05:08:37Z tommy $

Copyright (C) 2007-2008 TOMITA Masahiro

CacheManager

Author:TOMITA Masahiro <tommy@tmtm.org>
License:Ruby‘s. see www.ruby-lang.org/en/LICENSE.txt

Download

Install

 $ make
 # make install

Usage

 double = CacheManager.new do |key|
   puts "invoked"
   key * 2
 end
 double[1]   # => 2, invoked
 double[2]   # => 4, invoked
 double[1]   # => 2, not invoked

 three_obj_cache = CacheManager.new :limit=>3 do |key|
   true
 end
 three_obj_cache[1]
 three_obj_cache.cache  # => {1=>true}
 three_obj_cache[2]
 three_obj_cache.cache  # => {1=>true, 2=>true}
 three_obj_cache[3]
 three_obj_cache.cache  # => {1=>true, 2=>true, 3=>true}
 three_obj_cache[4]
 three_obj_cache.cache  # => {2=>true, 3=>true, 4=>true}

 cm = CacheManager.new
 cm[1]                   # => nil
 cm.get(1)               # => nil
 cm.get(1) {|key| key*3} # => 3
 cm[1]                   # => 3

[Validate]