Changeset 5456

Show
Ignore:
Timestamp:
01/25/08 02:27:59 (10 months ago)
Author:
takuya
Message:

/lang/php/Cache/Handler/Resource/ キャッシュをファイルに保存部分の修正

Location:
lang/php/Cache/trunk/Handler/Resource
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/php/Cache/trunk/Handler/Resource/File.php

    r3748 r5456  
    88 
    99require_once 'Cache/Handler/Resource/Interface.php'; 
    10 require_once 'Cache/Handler/require_once/File_csv_utils.php'; 
     10//CSVテスト中 
     11//require_once 'Cache/Handler/Resource/File_csv_utils.php'; 
     12//CSVを簡単に扱う 
     13//TODO:Groupファイルなど共有ファイルはロックが必要 
     14//     flock 関数の記述を参考にすると 
     15//     [いくつかのオーペレーティングシステムでflock() はプロセスレベルで実装されています。] 
     16//     とのこと。 
     17//     http://jp.php.net/manual/ja/function.flock.php 
     18// 
     19function file_put_csv( $filename,$lines, $file_mode="w", $delim=",",  $enclosure="\"" ) 
     20{ 
     21        $fp = @fopen( $filename, $file_mode ); 
     22        $state =null; 
     23        foreach( $lines as $idx => $line ){ 
     24                $state = fputcsv( $fp, $line, $delim, $enclosure ); 
     25        } 
     26        return $state; 
     27} 
     28//CSVを2次元配列に一括変換する関数 
     29function file_get_csv($filename, $length=100,$delim=",",$enclosure="\"") 
     30{ 
     31        $data = array(); 
     32        $fp = @fopen($filename,"r") /*or die("キャッシュファイルがない")*/; 
     33        while( $fp && ($buff=fgetcsv($fp,$length,$delim,$enclosure)) !==FALSE ){ 
     34                $data[]=$buff; 
     35        } 
     36        return $data; 
     37} 
     38//キャッシュを扱うBackEndクラス。Fileシステムにキャッシュを格納する 
     39//キャッシュは1エントリ=1ファイルが作成される 
     40//グループ管理のため グループ一覧ファイルが1ファイル作成される 
     41//更新時刻管理のため 最終更新時刻ファイルが1ファイル作成される 
    1142class Cache_Handler_Resource_File implements Cache_Handler_Resource_Interface { 
    1243    /**  
     
    124155    public function remove( $id ) 
    125156    { 
    126         $ret[] = unlink(  $this->getFullPath( $id ) ); 
     157        $ret[] = $this->removeID($id); 
    127158        $ret[] = $this->removeFromGroup( $id ); 
    128159        $ret[] = $this->removeModifiedIndex( $id ); 
    129160        $ret[] = $this->removeLifeTimeIndex( $id ); 
    130161        return ( $ret[0] && $ret[1] && $ret[2] && $ret[3] ); 
     162    } 
     163    private function removeID($id){ 
     164        return unlink( $this->getFullPath($id) ); 
    131165    } 
    132166    /**  
     
    250284        } 
    251285    } 
    252      
     286    //---for cache system management -- 
     287    //alisas for file_put_csv file_put_csvの記述場所をまだ決めていないから 
     288    public function saveList( $file_name, &$_array ){ 
     289        return file_put_csv($file_name, $_array); 
     290    } 
     291    //alisas for file_get_csv. file_get_csvの記述場所をまだ決めていないから 
     292    public function readList( $file_name ) 
     293    { 
     294        return file_get_csv( $file_name ); 
     295    } 
    253296    //---------group mangers--------- 
    254297     
     
    261304    public function getGroupList() 
    262305    { 
    263         $lines = @file_get_contents( $this->getGroupListFileName() ); 
    264         $array = array(); 
    265         foreach ( explode( "\n", $lines ) as $line ) { 
    266             if ( $line == "" ) { 
    267                 continue; 
    268             } 
    269             $array[] = explode( ",", $line ); 
    270         } 
    271         return $array; 
     306        return  $this->readList( $this->getGroupListFileName() ); 
    272307    } 
    273308     
     
    298333        $_array = $this->getGroupList(); 
    299334        $num   = sizeof( $_array ); 
    300         if ( $num == 0 ) { 
    301             $_array[] = array( $id, $groupname ); 
    302         } 
    303        foreach ( $groupnames as $groupname ) { 
    304             foreach ( $_array as $val ) { 
    305                 // $val[0] is cache id, $val[1] is group name 
    306                 if ( $val[0] == $id && $val[1] == $groupname ) { 
    307                     continue;//id is already exists in group 
    308                 } else { 
    309                     $_array[] = array( $id, $groupname ); 
    310                 } 
    311             } 
     335        foreach( $groupnames as $groupname ){ 
     336                $_array = $this->_addGroup($_array, $id, $groupname  ); 
    312337        } 
    313338        if ( $num != sizeof( $_array ) ) { 
     
    317342        } 
    318343    } 
    319     public function _addGroup( $id,$groupnames ){ 
    320         if ( !is_array( $groupnames ) ) { 
    321             $groupnames = array( $groupnames ); 
    322         } 
    323         $_array = fgetcsv( $this->getGroupListFileName() ); 
    324          
    325          
     344    public function _addGroup( &$_array, $id,$groupname ){ 
     345        $already_exists = false; 
     346        foreach($_array as $entry ){ 
     347                if( $entry[0] == $id && $entry[1] == $groupname  ){ 
     348                        $already_exists = true; 
     349                        break; 
     350                } 
     351        } 
     352        if( $already_exists === false){ 
     353                $_array[] = array( $id,$groupname ); 
     354        } 
     355        return $_array; 
    326356    } 
    327357    /**  
     
    343373    * @return boolean  true (succeeded) / false (faild) 
    344374    */ 
    345     public function removeGroup( $groupname, &$ids = null ) 
     375    public function removeGroup( $groupname ) 
    346376    { 
    347377        $list = $this->getGroupList(); 
    348378        //remove $group from group index file 
    349379        $buff = array(); 
    350         $ids  = array(); 
    351         foreach( $list as $val ){ 
    352             if( $val[1] == $groupname ){ 
    353                 $ids[] = $val[0]; 
    354             }else{ 
    355                 $buff[] = $val; 
    356             } 
    357         } 
    358         $ret[]= $this->saveGroupList( $buff ); 
    359         unset( $buff1 ); 
    360         //remove cache 
    361         if( sizeof($ids) == 0 ){ 
    362             return; 
    363         } 
    364          
    365         foreach( $ids as $id ){ 
    366             $ret[] = $this->remove( $id ); 
    367         } 
    368         // 
    369         $bool = true; 
    370         foreach( $ret as $b ){ 
    371             $bool = $bool && $b; 
    372         } 
    373         return $bool; 
     380        foreach( $list as $idx => $entry ){ 
     381                if($entry[1] == $groupname){ 
     382                        $buff[] = $entry[0];//削除IDを取り出す 
     383                        unset( $list[$idx] ); 
     384                } 
     385        } 
     386        //グループリストを更新する 
     387        $this->saveGroupList( $list ); 
     388        //ID一覧を元にTTLとModifiedを消す 
     389        $this->removeLifeTimeIndex($buff); 
     390        $this->removeModifiedIndex($buff); 
     391        //IDを消す 
     392        $this->removeID($id); 
    374393    } 
    375394     
     
    395414        $list = $this->getGroupList(); 
    396415        //remove from index 
    397         $buff = array(); 
    398         foreach( $list as $val ){ 
    399             if( $group != null && $group == $va[1] && $val[0] == $id ){ 
    400                 continue; 
    401             }else if( $group == null &&  $val[0] == $id){ 
    402                 continue; 
    403             }else{ 
    404                 $buff[] = $val; 
    405             } 
    406         } 
    407         return $this->saveGroupList( $buff ); 
     416        foreach( $list as $idx => $entry ){ 
     417                if($entry[0] == $id && ( $group == null ||  $entry[1] == $group) ){ 
     418                        unset( $entry[$idx] ); 
     419                } 
     420        } 
     421        return $this->saveGroupList( $list ); 
    408422     
    409423    } 
     
    414428    * @return boolean  true (succeeded) / false (faild) 
    415429    */ 
    416     public function saveGroupList( $list ) 
    417     { 
    418         foreach( $list as $line ){ 
    419             foreach( $line as $idx => $val ){ 
    420                 $line[$idx] = str_replace( ",", "", $line[$idx] );//remocve comma 
    421                 $line[$idx] = trim( $line[$idx] ); 
    422             } 
    423             $str .= implode( ",", $line ); 
    424             $str .= "\n"; 
    425         } 
    426         $ret = file_put_contents( $this->getGroupListFileName(), $str ); 
     430    public function saveGroupList( &$list ) 
     431    { 
     432        $ret = $this->saveList( $this->getGroupListFileName(), $list ); 
    427433        return ( $ret > 0 ); 
    428434    } 
     
    500506    /**  
    501507    * save list 
     508    * TODO::CSVの扱いを一元化する 
    502509    * @access private 
    503510    * @param array array of array( "id"=>"date" ) 
     
    577584    /**  
    578585    * save list 
     586    * TODO::CSVの扱いを一元化する 
    579587    * @access private 
    580588    * @param array array of array( "id"=>"date" ) 
  • lang/php/Cache/trunk/Handler/Resource/Interface.php

    r1523 r5456  
    77 */ 
    88 
    9 interface Cache_Handler_Resource_Interface{ 
     9interface Cache_Handler_Resource_Interface 
     10{ 
    1011     
    1112    /**  
  • lang/php/Cache/trunk/Handler/Resource/PDO.php

    r1698 r5456  
    236236    protected function _select( $id ) 
    237237    { 
     238        //ExpiredやGETで複数回同じQueryを出している問題点 
    238239        $_id = $this->escape( $id ); 
    239240        $sql = "SELECT * FROM cache_data WHERE id = $_id";