Class CacheManager
In: lib/cache_manager.rb
Parent: Object

$Id: cache_manager.rb 120 2007-12-20 22:59:26Z tommy $

Copyright (C) 2007 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}

Methods

[]   []=   cache   clear   delete   new  

Classes and Modules

Class CacheManager::Data

Public Class methods

opt:オプション (Hash)
block:Proc

CacheManager を初期化する。 self#[key] 時に key に対応する値がキャッシュされていない場合は、block が呼び出され、その戻り値がキャッシュされる。

opt の有効なキーは次のとおり。

:expire
Numeric。キャッシュ保持期間(秒数)。これより古いキャッシュは破棄される。
:limit
Integer。最大キャッシュ保持数。これより多く値が設定された場合は古いものから削除される。

Public Instance methods

key に対応する値を返す。値が存在しない場合は initialize に与えたブロックを実行して返す。

キャッシュのキー keyvalue を対応させる。value を返す。

{key => value} の Hash を返す。

キャッシュをクリアする。

キャッシュからキー key を削除する。

[Validate]