Changeset 22138 for lang/php/Cache
- Timestamp:
- 10/26/08 04:30:58 (3 months ago)
- Location:
- lang/php/Cache
- Files:
-
- 8 modified
-
ReadMe.txt (modified) (5 diffs, 1 prop)
-
sample.php (modified) (9 diffs)
-
trunk/Cache/CachedMethod.php (modified) (1 diff)
-
trunk/Cache/Handler.php (modified) (11 diffs)
-
trunk/Cache/Handler/IDGenerator.php (modified) (1 diff)
-
trunk/Cache/Handler/Item.php (modified) (3 diffs)
-
trunk/Cache/Utils.php (modified) (3 diffs)
-
trunk/tests/Cache_Test.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/php/Cache/ReadMe.txt
- Property svn:mime-type deleted
r6899 r22138 1 1 PHPでキャッシュを扱うクラスです。 2 2 3 P earのCacheはCache_LiteとCacheでインタフェースが違うので3 PEARのCacheはCache_LiteとCacheでインタフェースが違うので 4 4 相互運用性が低い。キャッシュの保存先を変えるとコードの 5 5 修正箇所が多い。 … … 7 7 なので、キャッシュの保存先を簡単に変えられるように、イン 8 8 タフェースを統一してみようかと。ついでに、キャッシュ保存 9 先データベースを分散出来たり、ミラーリングしたり出来 10 るようにしてある。9 先データベースを分散出来たり、ミラーリングしたり出来るよ 10 うにしてある。 11 11 12 12 … … 17 17 そういうのをやってます 18 18 19 Cache_Liteを改良してつくってたら、Zend_Cacheに同じようなものがあったのでorz 19 Cache_Liteを改良してつくってたら、Zend_Cacheに同じような 20 ものがあったのでorz 20 21 21 22 … … 24 25 RESTfulなURLを構造として持っておき 25 26 URLに紐付くデータをキャッシュとして保存しておく 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 ージ。 29 32 30 33 … … 32 35 RESTfulでURLがデータIDを示す構造が便利だと感じている。 33 36 34 キャッシュのキーをURNにすることで、このような構造を簡単に作れるはず。 37 キャッシュのキーをURNにすることで、このような構造を簡単 38 に作れるはず。 35 39 -
lang/php/Cache/sample.php
r6195 r22138 1 1 <?sphp 2 2 3 //Examples of Cache_Hander Class3 //Examples of Cache_Hander Class 4 4 //2008/02/04 12:45 更新 5 5 //written by hatena_id:takuya_1st … … 13 13 require_once "Cache/Utils.php"; 14 14 $data = "<html>sample</html>"; 15 cache_put_contents( "test_1", $data );//$_SERVER["tmp"]にキャッシュする15 cache_put_contents( "test_1", $data );//$_SERVER["tmp"]にキャッシュする 16 16 $data = cache_get_contents("test_1");//キャッシュしたデータを読み出す 17 17 … … 87 87 $handler = new Cache_Handler(); 88 88 $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])) ); 92 92 $handler->setResource( $res );//キャッシュの保存先を3つのPDOに分散する 93 93 if( $item->isExists() ){ … … 104 104 $handler = new Cache_Handler(); 105 105 $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 ); 109 109 $handler->setResource( $res );//キャッシュの保存先を優先度に従って分散する 110 110 if( $item->isExists() ){ … … 124 124 $ret[] = $cached->file_get_contents("http://www.yahoo.co.jp");//file_get_contentsの結果をキャッシュに保存 125 125 $ret[] = $cached->file_get_contents("http://www.yahoo.co.jp");//2回目以降はキャッシュが取り出される 126 $ret[0] == $ret[1] ; # true;126 $ret[0] == $ret[1]; # true; 127 127 128 128 … … 131 131 // 132 132 ////Very Simple Usage: 133 ////set resource 133 ////set resource 134 134 //Cache_Handler::setResource( new Cache_Handler_Resource_File(array("path"=>"./tmp")) ); 135 135 ////object … … 137 137 // 138 138 ////read cache 139 //if( $handler->isCached() ){ 139 //if( $handler->isCached() ){ 140 140 // return $handler->getCache(); 141 141 //} 142 142 ////caching data 143 //if( $cache_content){143 //if( $cache_content ){ 144 144 // $handler->setCache( $cache_content ); 145 145 // return $result = $handler->isCached();//success:True,failed:false 146 146 //} 147 147 ////remove cached data 148 //if( !$groups){148 //if( !$groups ){ 149 149 // //remove all 150 150 // $handler->clearAll(); 151 151 // $result = true; 152 //}else {152 //}else{ 153 153 // //remove cache by group 154 154 // foreach( $group as $name ){ … … 172 172 //$res_memcache_multi->addResource(new Cache_Handler_Resource_MemCache( array( 'host'=>'example.com', 'port'=> 11211 ) ) )); 173 173 // 174 ////Mirroring Cache 174 ////Mirroring Cache 175 175 //$res_mirror = new Cache_Handler_Resource_Mirror(); 176 176 //$res_mirror->addResource( $res_pdo_multi ); … … 182 182 //////use Plugin for Caching 183 183 ///////////////// 184 ////Gzip Compression Cache data184 ////Gzip Compression Cache data 185 185 //$plugin = new Ester_Cache_GzipPlugin(); 186 186 //$handler->addPlugin( $plugin ); 187 ////Save Cache_Data by BASE64 Encoding. 187 ////Save Cache_Data by BASE64 Encoding. 188 188 //$plugin = new Ester_Cache_Base64Plugin(); 189 189 //$handler->addPlugin( $plugin ); -
lang/php/Cache/trunk/Cache/CachedMethod.php
r7782 r22138 68 68 } 69 69 /////////TEST 70 if ( __FILE__ == $_SERVER["PHP_SELF"] ){70 if ( __FILE__ == $_SERVER["PHP_SELF"] ){ 71 71 //include_pathの解決 72 72 $ini_name = "include_path"; -
lang/php/Cache/trunk/Cache/Handler.php
r7782 r22138 1 1 <?php 2 /** 2 /** 3 3 * Cache Hanlder class. 4 4 * PHP version 5 … … 13 13 * @category Cache 14 14 */ 15 15 16 16 //コードをわかりやすくするためのClass 17 17 //このClassの利用でコードがSimpleになる 18 18 //$item = $Handler->CacheItem($id); 19 //if( $item->isExists()){19 //if( $item->isExists() ){ 20 20 // return $item->get(); 21 21 //}else{ … … 23 23 //} 24 24 //Cache_HandlerのAPIを一新した 25 require_once 'Cache/Handler/Resource/Interface.php';26 require_once 'Cache/Handler/Item.php';25 require_once "Cache/Handler/Resource/Interface.php"; 26 require_once "Cache/Handler/Item.php"; 27 27 class Cache_Handler{ 28 28 //ストレージから読み込んだデータを一時保存しておく 29 29 //TODO::多用するなら配列のサイズ制限をかけるべき 30 protected $temp =array();30 protected $temp = array(); 31 31 public function __construct(){ 32 $this->plugins = array();32 $this->plugins = array(); 33 33 } 34 34 //よく使うインスタンス化のパターンを記憶しておく 35 public static function factory($ini) {35 public static function factory($ini){ 36 36 $obj = new Cache_Handler(); 37 37 // … … 48 48 return $this->setPorp("plugins", $plugin); 49 49 } 50 //__set()の代用 50 //__set()の代用 51 51 //配列のproperty に値をセットする. 52 52 //引数が配列の場合と値の場合で処理を分ける。 53 53 //配列の場合はpropertyと配列をマージする。 54 54 //値の場合は、propertyに末尾に値を足す。 55 public function setPorp($prop_name, &$value){//参照渡し 55 public function setPorp($prop_name, &$value){//参照渡し 56 56 try{ 57 57 if( is_array($value) ){ 58 $this->$prop_name = @array_merge($this->$prop_name, $value);58 $this->$prop_name = @array_merge($this->$prop_name, $value); 59 59 }else{ 60 60 $this->{$prop_name}[] = $value; … … 87 87 //$ttlは文字列で指定することが出来る 88 88 $time_to_expired = false; 89 if( intval($data["ttl"])==0 ){89 if( intval($data["ttl"]) == 0 ){ 90 90 //$ttl が文字列で指定されている場合 91 91 //for example : $ttl ="+ 7day","next Thursday" など 92 92 $time_to_expired = strtotime( $data["ttl"], strtotime($data["modifed"]) ); 93 93 }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 97 97 if( $time_to_expired === false ){//文字列が不正なとき 98 98 $time_to_expired = time()-3600;//有効期限切れとする … … 143 143 } 144 144 //protected function setCacheProp($id,$prop_name){} 145 protected function updateCache($id,$data) {145 protected function updateCache($id,$data){ 146 146 //このクラスがキャッシュしているTEMPを更新する 147 147 //プラグインを引っかける 148 $content = $this->beforeWrite( $data );148 $content = $this->beforeWrite( $data ); 149 149 //Resouceへ保存 150 150 $ret = $this->resource->save( $content, $id ); … … 152 152 } 153 153 //キャッシュをドライバから読み出す、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 ){ 156 156 $data = $this->temp; 157 }else {157 }else{ 158 158 $data["id"] = $id; 159 159 $data["content"] = $this->resource->get( $id ); … … 162 162 $data["content"] = $this->afterRead( $data["content"] ); 163 163 //このクラスのtemporaryへ 164 $this->temp =$data;164 $this->temp = $data; 165 165 } 166 166 return $data["content"]; … … 171 171 return $this->temp["modifed"]; 172 172 } 173 /** 174 * execute plugins175 * @access protected173 /** 174 * execute plugins 175 * @access protected 176 176 */ 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 plugins189 * @access protected190 */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 } 201 201 } 202 202 203 203 ////TEST 204 //if (__FILE__ == $_SERVER["PHP_SELF"] ){204 //if( __FILE__ == $_SERVER["PHP_SELF"] ){ 205 205 206 206 //require_once "Handler.php"; … … 210 210 211 211 ////キャッシュの設定 212 //$handler = new Cache_Handler;212 //$handler = new Cache_Handler; 213 213 //$handler->addResource( new Cache_Handler_Resource_File(array("path"=>"./tmp")) ); 214 //$handler->addPlugin( new Cache_Handler_PlugIn_Gzip () );//Gzip Compression Cache data215 //$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. 216 216 217 217 218 218 //$cache_items = Cache_Handler::factory(); 219 219 ////このような記述が出来ると嬉しいよね 220 //if( $cache->isExists($id)){220 //if( $cache->isExists($id) ){ 221 221 ////cache_is_Expiredでキャッシュの存在時点とライフサイクルをチェックする。かつキャッシュを読み込んでおく 222 222 //return $cahce->getCache($id); 223 223 //}else{ 224 ////do something 224 ////do something 225 225 //$data; 226 226 //$cache->setCache($id,$data); … … 236 236 ////キャッシュの設定は一意だと仮定する? 237 237 //class Cache_Handler_ { 238 // 239 ///** 240 //* Cache resource 241 //* @access public242 //* @type Cache_Handler_Resource_Interface243 //*/244 //public static $resource;245 ///** 246 //* Cached data filtering plugins 247 //* @access public248 //* @type Cache_Handler_Resource_Interface249 //*/250 //public static $plugin;251 ///** 252 //* constructor 253 //* @access public 254 //* @param array $ini array( "property name" => "value " );255 //* @return void256 //*/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 static269 //* @access public 270 //* @type Cache_Handler_Resource_Interface271 //* @return void272 //*/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 static279 //* @access public 280 //* @return Cache_Handler_Resource_Interface281 //*/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 resource298 //* @return boolean true (succeeded) / false (faild)299 //*/300 //public static function clearAll()301 //{302 //self::$resource->clean();303 //}304 ///** 305 //* Add plugin to this class306 //* plugin is executed by added order307 //* @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 //* Unregistplugin323 //* @access public 324 //* @param int $index order of plugin325 //*/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 plugin334 //* @return Cache_Handler_PlugIn_Interface plugin335 //*/336 //public function & getPlugin($index)337 //{338 //return $this->plugin[$index];339 //}340 ///** 341 //* execute plugin342 //* @access protected343 //*/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 plugin356 //* @access protected357 //*/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_id371 //* @access public 372 //* @param void373 //* @return void374 //*/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 public400 //* @param String $time, cache life time401 //* @return boolean402 //*/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 expired416 //*/417 //public function isExpired()418 //{419 //$modifed = $this->getLastModified();420 //$lifetime = $this->getCacheLifeTime();421 //if( $lifetime == null ){//eternal cache422 //return false;423 //}else{424 //return time() >= strtotime( $modified )+$lifetime;425 //}426 //}427 ///** 428 //* return true if cache is NOT expired429 //*/430 //public function isNotExpired()431 //{432 //return !$this->isExpired();433 //}434 ///** 435 //* get cache of this cache id, and plugin applied436 //* @access public 437 //* @param void438 //* @return String cache data439 //*/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 id472 //* @access public 473 //* @param String $id cache id474 //* @return void475 //*/476 //public function setCacheId( $id )477 //{478 //$this->cache_id = $id;479 //}480 ///** 481 //* returns cache id482 //* @access public 483 //* @param void 484 //* @return String cache id485 //*/486 //public function getCacheId()487 //{488 //return $this->cache_id;489 //}490 ///** 491 //* add cache group name to this cache id492 //* @access public 493 //* @param String $name494 //* @return void495 //*/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 //* @access509 //* @param510 //* @return511 //*/512 //public function setCacheGroupName( $names )513 //{
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)