Changeset 22138 for lang/php/Cache

Show
Ignore:
Timestamp:
10/26/08 04:30:58 (3 months ago)
Author:
drry
Message:
  • fixed typos.
  • removed a subversion property.
  • et cetera.
Location:
lang/php/Cache
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • lang/php/Cache/ReadMe.txt

    • Property svn:mime-type deleted
    r6899 r22138  
    11PHPでキャッシュを扱うクラスです。 
    22 
    3 PearのCacheはCache_LiteとCacheでインタフェースが違うので 
     3PEARのCacheはCache_LiteとCacheでインタフェースが違うので 
    44相互運用性が低い。キャッシュの保存先を変えるとコードの 
    55修正箇所が多い。 
     
    77なので、キャッシュの保存先を簡単に変えられるように、イン 
    88タフェースを統一してみようかと。ついでに、キャッシュ保存 
    9 先データベースを分散出来たり、ミラーリングしたり出来 
    10 るようにしてある。 
     9先データベースを分散出来たり、ミラーリングしたり出来るよ 
     10うにしてある。 
    1111 
    1212 
     
    1717そういうのをやってます 
    1818 
    19 Cache_Liteを改良してつくってたら、Zend_Cacheに同じようなものがあったのでorz 
     19Cache_Liteを改良してつくってたら、Zend_Cacheに同じような 
     20ものがあったのでorz 
    2021 
    2122 
     
    2425RESTfulなURLを構造として持っておき 
    2526URLに紐付くデータをキャッシュとして保存しておく 
    26 {cache_key =>URL, cache_data=> object} 
    27 のを作り、ユーザーはobjectへHTTPメソッド操作をするようにする。 
    28 そういうイメージで2層構造+バックエンドの3層構造をイメージ。 
     27{cache_key => URL, cache_data => object} 
     28のを作り、ユーザーはobjectへHTTPメソッド操作をするように 
     29する。 
     30そういうイメージで2層構造+バックエンドの3層構造をイメ 
     31ージ。 
    2932 
    3033 
     
    3235RESTfulでURLがデータIDを示す構造が便利だと感じている。 
    3336 
    34 キャッシュのキーをURNにすることで、このような構造を簡単に作れるはず。 
     37キャッシュのキーをURNにすることで、このような構造を簡単 
     38に作れるはず。 
    3539 
  • lang/php/Cache/sample.php

    r6195 r22138  
    11<?sphp 
    22 
    3 //Examples of  Cache_Hander Class 
     3//Examples of Cache_Hander Class 
    44//2008/02/04 12:45 更新 
    55//written by hatena_id:takuya_1st 
     
    1313require_once "Cache/Utils.php"; 
    1414$data = "<html>sample</html>"; 
    15 cache_put_contents( "test_1",  $data );//$_SERVER["tmp"]にキャッシュする 
     15cache_put_contents( "test_1", $data );//$_SERVER["tmp"]にキャッシュする 
    1616$data = cache_get_contents("test_1");//キャッシュしたデータを読み出す 
    1717 
     
    8787$handler = new Cache_Handler(); 
    8888$res = new Cache_Handler_Resource_Multiple();//キャッシュの保存先を分割する 
    89 $res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[0]))  ); 
    90 $res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[1]))  ); 
    91 $res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[3]))  ); 
     89$res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[0])) ); 
     90$res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[1])) ); 
     91$res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[3])) ); 
    9292$handler->setResource( $res );//キャッシュの保存先を3つのPDOに分散する 
    9393if( $item->isExists() ){ 
     
    104104$handler = new Cache_Handler(); 
    105105$res = new Cache_Handler_Resource_RoundRobin ();//キャッシュの保存先を分割する 
    106 $res->addResource( new Cache_Handler_Resource_File("./tmp") , 1 ); 
    107 $res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[0])) , 2 ); 
    108 $res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[1])) , 3 ); 
     106$res->addResource( new Cache_Handler_Resource_File("./tmp"), 1 ); 
     107$res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[0])), 2 ); 
     108$res->addResource( new Cache_Handler_Resource_PDO(array("dsn"=>$dsn[1])), 3 ); 
    109109$handler->setResource( $res );//キャッシュの保存先を優先度に従って分散する 
    110110if( $item->isExists() ){ 
     
    124124$ret[] = $cached->file_get_contents("http://www.yahoo.co.jp");//file_get_contentsの結果をキャッシュに保存 
    125125$ret[] = $cached->file_get_contents("http://www.yahoo.co.jp");//2回目以降はキャッシュが取り出される 
    126 $ret[0] == $ret[1] ; # true; 
     126$ret[0] == $ret[1]; # true; 
    127127 
    128128 
     
    131131// 
    132132////Very Simple Usage: 
    133 ////set resource  
     133////set resource 
    134134//Cache_Handler::setResource( new Cache_Handler_Resource_File(array("path"=>"./tmp")) ); 
    135135////object 
     
    137137// 
    138138////read cache 
    139 //if( $handler->isCached() ){    
     139//if( $handler->isCached() ){ 
    140140//      return $handler->getCache(); 
    141141//} 
    142142////caching data 
    143 //if($cache_content){ 
     143//if( $cache_content ){ 
    144144//      $handler->setCache( $cache_content ); 
    145145//      return $result = $handler->isCached();//success:True,failed:false 
    146146//} 
    147147////remove cached data 
    148 //if(!$groups){ 
     148//if( !$groups ){ 
    149149//      //remove all 
    150150//      $handler->clearAll(); 
    151151//      $result = true; 
    152 //}else { 
     152//}else{ 
    153153//      //remove cache by group 
    154154//      foreach( $group as $name ){ 
     
    172172//$res_memcache_multi->addResource(new Cache_Handler_Resource_MemCache( array( 'host'=>'example.com', 'port'=> 11211 ) ) )); 
    173173// 
    174 ////Mirroring Cache  
     174////Mirroring Cache 
    175175//$res_mirror = new Cache_Handler_Resource_Mirror(); 
    176176//$res_mirror->addResource( $res_pdo_multi ); 
     
    182182//////use Plugin for Caching 
    183183///////////////// 
    184 ////Gzip Compression  Cache data 
     184////Gzip Compression Cache data 
    185185//$plugin = new Ester_Cache_GzipPlugin(); 
    186186//$handler->addPlugin( $plugin ); 
    187 ////Save Cache_Data by BASE64 Encoding.  
     187////Save Cache_Data by BASE64 Encoding. 
    188188//$plugin = new Ester_Cache_Base64Plugin(); 
    189189//$handler->addPlugin( $plugin ); 
  • lang/php/Cache/trunk/Cache/CachedMethod.php

    r7782 r22138  
    6868} 
    6969/////////TEST 
    70 if (  __FILE__ == $_SERVER["PHP_SELF"] ){ 
     70if ( __FILE__ == $_SERVER["PHP_SELF"] ){ 
    7171//include_pathの解決 
    7272$ini_name = "include_path"; 
  • lang/php/Cache/trunk/Cache/Handler.php

    r7782 r22138  
    11<?php 
    2 /**  
     2/** 
    33 * Cache Hanlder class. 
    44 * PHP version 5 
     
    1313 * @category    Cache 
    1414 */ 
    15   
     15 
    1616//コードをわかりやすくするためのClass 
    1717//このClassの利用でコードがSimpleになる 
    1818//$item = $Handler->CacheItem($id); 
    19 //if($item->isExists()){ 
     19//if( $item->isExists() ){ 
    2020//      return $item->get(); 
    2121//}else{ 
     
    2323//} 
    2424//Cache_HandlerのAPIを一新した 
    25 require_once 'Cache/Handler/Resource/Interface.php'; 
    26 require_once 'Cache/Handler/Item.php'; 
     25require_once "Cache/Handler/Resource/Interface.php"; 
     26require_once "Cache/Handler/Item.php"; 
    2727class Cache_Handler{ 
    2828        //ストレージから読み込んだデータを一時保存しておく 
    2929        //TODO::多用するなら配列のサイズ制限をかけるべき 
    30         protected $temp=array(); 
     30        protected $temp = array(); 
    3131        public function __construct(){ 
    32                 $this->plugins  = array(); 
     32                $this->plugins = array(); 
    3333        } 
    3434        //よく使うインスタンス化のパターンを記憶しておく 
    35         public static function factory($ini) { 
     35        public static function factory($ini){ 
    3636                $obj = new Cache_Handler(); 
    3737                // 
     
    4848                return $this->setPorp("plugins", $plugin); 
    4949        } 
    50         //__set()の代用  
     50        //__set()の代用 
    5151        //配列のproperty に値をセットする. 
    5252        //引数が配列の場合と値の場合で処理を分ける。 
    5353        //配列の場合はpropertyと配列をマージする。 
    5454        //値の場合は、propertyに末尾に値を足す。 
    55         public function setPorp($prop_name, &$value){//参照渡し  
     55        public function setPorp($prop_name, &$value){//参照渡し 
    5656                try{ 
    5757                        if( is_array($value) ){ 
    58                                 $this->$prop_name =  @array_merge($this->$prop_name, $value); 
     58                                $this->$prop_name = @array_merge($this->$prop_name, $value); 
    5959                        }else{ 
    6060                                $this->{$prop_name}[] = $value; 
     
    8787                //$ttlは文字列で指定することが出来る 
    8888                $time_to_expired = false; 
    89                 if(intval($data["ttl"])==0 ){ 
     89                if( intval($data["ttl"]) == 0 ){ 
    9090                        //$ttl が文字列で指定されている場合 
    9191                        //for example : $ttl ="+ 7day","next Thursday" など 
    9292                        $time_to_expired = strtotime( $data["ttl"], strtotime($data["modifed"]) ); 
    9393                }else if( is_numeric(intval($data["ttl"])) ){ 
    94                         $time_to_expired  = strtotime( $data["modifed"] )+$data["ttl"]; 
    95                 } 
    96                  
     94                        $time_to_expired = strtotime( $data["modifed"] )+$data["ttl"]; 
     95                } 
     96 
    9797                if( $time_to_expired === false ){//文字列が不正なとき 
    9898                        $time_to_expired = time()-3600;//有効期限切れとする 
     
    143143        } 
    144144        //protected function setCacheProp($id,$prop_name){} 
    145         protected function updateCache($id,$data) { 
     145        protected function updateCache($id,$data){ 
    146146                //このクラスがキャッシュしているTEMPを更新する 
    147147                //プラグインを引っかける 
    148                 $content =  $this->beforeWrite( $data ); 
     148                $content = $this->beforeWrite( $data ); 
    149149                //Resouceへ保存 
    150150                $ret = $this->resource->save( $content, $id ); 
     
    152152        } 
    153153        //キャッシュをドライバから読み出す、Templateメソッド 
    154         protected function readCache($id) { 
    155                 if( isset($this->temp["id"]) && $this->temp["id"] === $id ) { 
     154        protected function readCache($id){ 
     155                if( isset($this->temp["id"]) && $this->temp["id"] === $id ){ 
    156156                        $data = $this->temp; 
    157                 }else { 
     157                }else{ 
    158158                        $data["id"]              = $id; 
    159159                        $data["content"] = $this->resource->get( $id ); 
     
    162162                        $data["content"] = $this->afterRead( $data["content"] ); 
    163163                        //このクラスのtemporaryへ 
    164                         $this->temp=$data; 
     164                        $this->temp = $data; 
    165165                } 
    166166                return $data["content"]; 
     
    171171                return $this->temp["modifed"]; 
    172172        } 
    173     /**  
    174     * execute plugins 
    175     * @access protected 
     173        /** 
     174        * execute plugins 
     175        * @access protected 
    176176        */ 
    177     protected function beforeWrite( $data ) 
    178     { 
    179         if( $data == null ){ 
    180             return; 
    181         } 
    182         foreach( $this->plugins as $modifer ){ 
    183             $data = $modifer->beforeWrite( $data ); 
    184         } 
    185         return $data; 
    186     } 
    187     /**  
    188     * execute plugins 
    189     * @access protected 
    190     */ 
    191     protected function afterRead( $data ) 
    192     { 
    193         if( $data == null ){  
    194             return; 
    195         } 
    196         foreach( array_reverse( $this->plugins ) as $modifer ){ 
    197             $data = $modifer->afterRead( $data ); 
    198         } 
    199         return $data; 
    200     } 
     177        protected function beforeWrite( $data ) 
     178        { 
     179                if( $data == null ){ 
     180                        return; 
     181                } 
     182                foreach( $this->plugins as $modifer ){ 
     183                        $data = $modifer->beforeWrite( $data ); 
     184                } 
     185                return $data; 
     186        } 
     187        /** 
     188        * execute plugins 
     189        * @access protected 
     190        */ 
     191        protected function afterRead( $data ) 
     192        { 
     193                if( $data == null ){ 
     194                        return; 
     195                } 
     196                foreach( array_reverse( $this->plugins ) as $modifer ){ 
     197                        $data = $modifer->afterRead( $data ); 
     198                } 
     199                return $data; 
     200        } 
    201201} 
    202202 
    203203////TEST 
    204 //if ( __FILE__ == $_SERVER["PHP_SELF"] ){ 
     204//if( __FILE__ == $_SERVER["PHP_SELF"] ){ 
    205205 
    206206//require_once "Handler.php"; 
     
    210210 
    211211////キャッシュの設定 
    212 //$handler= new Cache_Handler; 
     212//$handler = new Cache_Handler; 
    213213//$handler->addResource( new Cache_Handler_Resource_File(array("path"=>"./tmp")) ); 
    214 //$handler->addPlugin( new Cache_Handler_PlugIn_Gzip () );//Gzip Compression  Cache data 
    215 //$handler->addPlugin( new Cache_Handler_PlugIn_Base64() );//Save Cache_Data by BASE64 Encoding.  
     214//$handler->addPlugin( new Cache_Handler_PlugIn_Gzip () );//Gzip Compression Cache data 
     215//$handler->addPlugin( new Cache_Handler_PlugIn_Base64() );//Save Cache_Data by BASE64 Encoding. 
    216216 
    217217 
    218218//$cache_items = Cache_Handler::factory(); 
    219219////このような記述が出来ると嬉しいよね 
    220 //if($cache->isExists($id)){ 
     220//if( $cache->isExists($id) ){ 
    221221////cache_is_Expiredでキャッシュの存在時点とライフサイクルをチェックする。かつキャッシュを読み込んでおく 
    222222        //return $cahce->getCache($id); 
    223223//}else{ 
    224         ////do something  
     224        ////do something 
    225225        //$data; 
    226226        //$cache->setCache($id,$data); 
     
    236236////キャッシュの設定は一意だと仮定する? 
    237237//class Cache_Handler_ { 
    238    //  
    239     ///**  
    240     //* Cache resource  
    241     //* @access public 
    242     //* @type   Cache_Handler_Resource_Interface 
    243     //*/ 
    244     //public static $resource; 
    245     ///**  
    246     //* Cached data filtering plugins  
    247     //* @access public 
    248     //* @type   Cache_Handler_Resource_Interface 
    249     //*/ 
    250     //public static $plugin; 
    251     ///**  
    252     //* constructor  
    253     //* @access public  
    254     //* @param array $ini array( "property name" => "value " ); 
    255     //* @return void 
    256     //*/ 
    257     //public function __construct($id, $group) 
    258     //{ 
    259         //$this->resource =& self::$resource; 
    260         //$this->setCacheId( $id ); 
    261         //$this->setCacheGroupName( $group ); 
    262         //$this->plugin =& self::$plugin; 
    263     //} 
    264     //public function CacheItem($id, $group){ 
    265     //   
    266     //} 
    267     ///**  
    268     //* set Cache resource to this cache handler as static 
    269     //* @access public  
    270     //* @type   Cache_Handler_Resource_Interface 
    271     //* @return void 
    272     //*/ 
    273     //public static function setResource( Cache_Handler_Resource_Interface $res ) 
    274     //{ 
    275         //self::$resource = $res; 
    276     //} 
    277     ///**  
    278     //* get Cache resource to this cache handler as static 
    279     //* @access public  
    280     //* @return   Cache_Handler_Resource_Interface 
    281     //*/ 
    282     //public static function getResource() 
    283     //{ 
    284         //return self::$resource; 
    285     //} 
    286     ///**  
    287     //* Clear Cache group from this resource  
    288     //* @access public  
    289     //* @param  String group name  
    290     //* @return boolean  true (succeeded) / false (faild) 
    291     //*/ 
    292     //public static function clearCacheGroup( $name ) 
    293     //{ 
    294         //return self::$resource->clean( $name ); 
    295     //} 
    296     ///**  
    297     //* clear all cache from this resource 
    298     //* @return boolean  true (succeeded) / false (faild) 
    299     //*/ 
    300     //public static function clearAll() 
    301     //{ 
    302         //self::$resource->clean(); 
    303     //} 
    304     ///**  
    305     //* Add plugin to this class 
    306     //* plugin is executed by added order 
    307     //* @access public  
    308     //*/ 
    309     //public static function addPlugin( Cache_Handler_PlugIn_Interface $plugin ) 
    310     //{ 
    311         //self::$plugin[] =$plugin; 
    312     //} 
    313     ///**  
    314     //* clear all registed plugin. 
    315     //* @access public  
    316     //*/ 
    317     //public static function clearPlugin() 
    318     //{ 
    319         //unset( self::$plugin ); 
    320     //} 
    321     ///**  
    322     //* Unregist plugin 
    323     //* @access public  
    324     //* @param int $index  order of plugin 
    325     //*/ 
    326     //public static function delPlugin( $index ) 
    327     //{ 
    328         //unset( self::$plugin[$index] ); 
    329     //} 
    330     ///**  
    331     //* Retrieve registered plugin  
    332     //* @access public  
    333     //* @param int $index  order of plugin 
    334     //* @return Cache_Handler_PlugIn_Interface plugin 
    335     //*/ 
    336     //public function & getPlugin($index) 
    337     //{ 
    338         //return $this->plugin[$index]; 
    339     //} 
    340     ///**  
    341     //* execute plugin 
    342     //* @access protected 
    343     //*/ 
    344     //protected function beforeWrite( $data ) 
    345     //{ 
    346         //if( $data == null ){ 
    347             //return; 
    348         //} 
    349         //foreach( $this->plugin as $modifer ){ 
    350             //$data = $modifer->beforeWrite( $data ); 
    351         //} 
    352         //return $data; 
    353     //} 
    354     ///**  
    355     //* execute plugin 
    356     //* @access protected 
    357     //*/ 
    358     //protected function afterRead( $data ) 
    359     //{ 
    360         //if( $data == null ){  
    361             //return; 
    362         //} 
    363         //foreach( array_reverse( $this->plugin ) as $modifer ){ 
    364             //$data = $modifer->afterRead( $data ); 
    365         //} 
    366         //return $data; 
    367     //} 
    368    //  
    369     ///** 
    370     //* clear cache of this cache_id 
    371     //* @access public  
    372     //* @param void 
    373     //* @return void 
    374     //*/ 
    375     //public function clear() 
    376     //{ 
    377         //$this->resource->remove( $this->cache_id ); 
    378     //} 
    379     ///**  
    380     //* check is this cache_id cached in resource. 
    381     //* @access private  
    382     //* @return boolean  
    383     //*/ 
    384     //public function isCached( $date = null ) 
    385     //{ 
    386         //if( $this->_data == null ){ 
    387             //$this->_data = $this->getCache(); 
    388         //} 
    389        //  
    390         //if( $this->_data === FALSE || $this->_data === null || $this->_data == ""){ 
    391             //return false; 
    392         //}else{ 
    393             //return true; 
    394         //} 
    395     //} 
    396     ///**  
    397     //* Check cache is not expired. 
    398     //* If cache life time is expired, clear cache and return false. 
    399     //* @access public 
    400     //* @param String $time, cache life time 
    401     //* @return boolean 
    402     //*/ 
    403     //public function check() 
    404     //{ 
    405         //if( $this->isCached( $date ) ){ 
    406             //return true; 
    407         //}else{ 
    408             //if( $this->isExpired() ){ 
    409                 //$this->clear(); 
    410             //} 
    411             //return false; 
    412         //} 
    413     //} 
    414     ///**  
    415     //* return true if cache is expired 
    416     //*/ 
    417     //public function isExpired() 
    418     //{ 
    419         //$modifed  = $this->getLastModified(); 
    420         //$lifetime = $this->getCacheLifeTime(); 
    421         //if( $lifetime == null ){//eternal cache 
    422             //return false; 
    423         //}else{ 
    424             //return time() >= strtotime( $modified )+$lifetime; 
    425         //} 
    426     //} 
    427     ///**  
    428     //* return true if cache is NOT expired 
    429     //*/ 
    430     //public function isNotExpired() 
    431     //{ 
    432         //return !$this->isExpired(); 
    433     //} 
    434     ///**  
    435     //* get cache of this cache id, and plugin applied 
    436     //* @access public  
    437     //* @param void 
    438     //* @return String cache data 
    439     //*/ 
    440     //public function getCache() 
    441     //{ 
    442         //$data = $this->resource->get( $this->cache_id ); 
    443         //$data = $this->afterRead( $data ); 
    444         //return $data; 
    445     //} 
    446     ///**  
    447     //* Return cached modified date of this cache id. 
    448     //* When resource has no cache, this function returns null. 
    449     //* @return String date  
    450     //*/ 
    451     //public function getLastModified() 
    452     //{ 
    453         //if( $this->isCached() == false ){ 
    454             //return null; 
    455         //} 
    456         //return $this->resource->getLastModified( $this->cache_id ); 
    457     //} 
    458     ///**  
    459     //* return cached life time of this cache id. 
    460     //* when resource has no cache, this function returns null. 
    461     //* @return int  
    462     //*/ 
    463     //public function getCacheLifeTime() 
    464     //{ 
    465         //if( $this->isCached() == false ){ 
    466             //return null; 
    467         //} 
    468         //return $this->resource->getTimeToLive( $this->cache_id ); 
    469     //} 
    470     ///**  
    471     //* set this cache id 
    472     //* @access public  
    473     //* @param String $id cache id 
    474     //* @return void 
    475     //*/ 
    476     //public function setCacheId( $id ) 
    477     //{ 
    478         //$this->cache_id = $id; 
    479     //} 
    480     ///**  
    481     //* returns cache id 
    482     //* @access public  
    483     //* @param void  
    484     //* @return String cache id 
    485     //*/ 
    486     //public function getCacheId() 
    487     //{ 
    488         //return $this->cache_id; 
    489     //} 
    490     ///**  
    491     //* add cache group name to this cache id 
    492     //* @access public  
    493     //* @param String $name 
    494     //* @return void 
    495     //*/ 
    496     //public function addCacheGroupName( $name ) 
    497     //{ 
    498         //if( $name != "" ){ 
    499             ////$ret = $this->resource->addIntoGroup( $this->cache_id, $name ); 
    500             //$this->group[] = $name; 
    501             //return true; 
    502         //} 
    503         //return false; 
    504     //} 
    505    //  
    506     ///**  
    507     //* resets cache group name already set, and set cache group name. 
    508     //* @access 
    509     //* @param 
    510     //* @return 
    511     //*/ 
    512     //public function setCacheGroupName( $names ) 
    513     //{