Changeset 5456
- Timestamp:
- 01/25/08 02:27:59 (10 months ago)
- Location:
- lang/php/Cache/trunk/Handler/Resource
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/php/Cache/trunk/Handler/Resource/File.php
r3748 r5456 8 8 9 9 require_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 // 19 function 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次元配列に一括変換する関数 29 function 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ファイル作成される 11 42 class Cache_Handler_Resource_File implements Cache_Handler_Resource_Interface { 12 43 /** … … 124 155 public function remove( $id ) 125 156 { 126 $ret[] = unlink( $this->getFullPath( $id ));157 $ret[] = $this->removeID($id); 127 158 $ret[] = $this->removeFromGroup( $id ); 128 159 $ret[] = $this->removeModifiedIndex( $id ); 129 160 $ret[] = $this->removeLifeTimeIndex( $id ); 130 161 return ( $ret[0] && $ret[1] && $ret[2] && $ret[3] ); 162 } 163 private function removeID($id){ 164 return unlink( $this->getFullPath($id) ); 131 165 } 132 166 /** … … 250 284 } 251 285 } 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 } 253 296 //---------group mangers--------- 254 297 … … 261 304 public function getGroupList() 262 305 { 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() ); 272 307 } 273 308 … … 298 333 $_array = $this->getGroupList(); 299 334 $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 ); 312 337 } 313 338 if ( $num != sizeof( $_array ) ) { … … 317 342 } 318 343 } 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; 326 356 } 327 357 /** … … 343 373 * @return boolean true (succeeded) / false (faild) 344 374 */ 345 public function removeGroup( $groupname , &$ids = null)375 public function removeGroup( $groupname ) 346 376 { 347 377 $list = $this->getGroupList(); 348 378 //remove $group from group index file 349 379 $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); 374 393 } 375 394 … … 395 414 $list = $this->getGroupList(); 396 415 //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 ); 408 422 409 423 } … … 414 428 * @return boolean true (succeeded) / false (faild) 415 429 */ 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 ); 427 433 return ( $ret > 0 ); 428 434 } … … 500 506 /** 501 507 * save list 508 * TODO::CSVの扱いを一元化する 502 509 * @access private 503 510 * @param array array of array( "id"=>"date" ) … … 577 584 /** 578 585 * save list 586 * TODO::CSVの扱いを一元化する 579 587 * @access private 580 588 * @param array array of array( "id"=>"date" ) -
lang/php/Cache/trunk/Handler/Resource/Interface.php
r1523 r5456 7 7 */ 8 8 9 interface Cache_Handler_Resource_Interface{ 9 interface Cache_Handler_Resource_Interface 10 { 10 11 11 12 /** -
lang/php/Cache/trunk/Handler/Resource/PDO.php
r1698 r5456 236 236 protected function _select( $id ) 237 237 { 238 //ExpiredやGETで複数回同じQueryを出している問題点 238 239 $_id = $this->escape( $id ); 239 240 $sql = "SELECT * FROM cache_data WHERE id = $_id";
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)