Changeset 2153 for lang/php

Show
Ignore:
Timestamp:
11/30/07 01:36:53 (5 years ago)
Author:
takuya
Message:

/lang/php/Cache キャッシュ保存先への負荷分散として重み付け出来るようにした

Files:
1 modified

Legend:

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

    r2081 r2153  
    66 *<code> 
    77 * //example for multiple cache 
    8  * $multi = new Pear_Cache_Handler_Resource_Mirror(); 
    9  * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[0]) ); 
    10  * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[1]) ); 
    11  * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[2]) ); 
    12  * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[3]) ); 
    13  * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[4]) ); 
     8 * $multi = new Cache_Handler_Resource_RoundRobin(); 
     9 * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[0]),1 ); 
     10 * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[1]),2 ); 
     11 * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[2]),1 ); 
     12 * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[3]),3 ); 
     13 * $multi->addResource( new Pear_Cache_Handler_Resource_MemCached($ini[4]),1 ); 
    1414 * //add multile to handler 
    15  * $handler = new Pear_Cache_Handler($id); 
    16  * $handler->setResource( $multi ); 
     15 * Cache_Handler::setResource( $multi ); 
     16 * //stores data 
     17 * $handler = new Cache_Handler($id); 
    1718 * $handler->setCache( $data ); 
    1819 *<code> 
     
    2021 * @category    Cache 
    2122 */ 
    22  
    23 require_once 'Cache/Handler/Resource/Mirror.php'; 
    24  
    25  
    26 class Cache_Handler_Resource_RoundRobin extends Cache_Handler_Resource_Mirror 
     23require_once 'Cache/Handler/Resource/Interface.php'; 
     24require_once 'Cache/Handler/Resource/Multiple.php'; 
     25 
     26 
     27class Cache_Handler_Resource_RoundRobin implements Cache_Handler_Resource_Interface 
    2728{ 
    28                  
    29         private $weight = array(); 
    30         public function setWeight( $index, $weight ){ 
    31                 if( is_array( $index ) ){ 
    32                         $this->weight = $index; 
    33                 }else{ 
    34                         $this->weight[$index] = $weight; 
    35                 } 
    36         } 
     29        private $mapping   = array(); 
     30        private $resources = array(); 
     31    /**  
     32    * constructor 
     33    * @access public 
     34    * @param $ini array of string for config. 
     35    */ 
     36    public function __construct( $ini ) 
     37    { 
     38        foreach( $ini as $key => $val ){ 
     39            $this->$key = $val; 
     40        } 
     41    } 
     42    /**  
     43    * add resource instance 
     44    * @access public  
     45    * @param Cache_Handler_Resource resource 
     46    * @param int Weigth  
     47    * @return 
     48    */ 
     49    public function addResource( $resourceObj, $weight=1 ) 
     50    { 
     51        $this->resources[] = $resourceObj; 
     52        for( $i=0;$i<$weight;$i++){ 
     53                $this->mapping[] = sizeof($this->resources); 
     54        } 
     55    } 
     56    public function setResource( $resource = null ) 
     57    { 
     58        if( is_array($resource) ){ 
     59            $this->resources = $resource; 
     60        }else{ 
     61            $this->resources   = array(); 
     62            $this->addResource( $resource ); 
     63        } 
     64    } 
     65    public function getResource( $idx ) 
     66    { 
     67        return $this->resources[$idx]; 
     68    } 
     69    /** 
     70    * use getTarget instead. 
     71    */ 
     72    protected function _target( $id = null ) 
     73    { 
     74             $i = intval(rand( 0, sizeof($this->mapping))); 
     75             return $this->resource[$this->mapping[$i]]; 
     76    } 
     77    /**  
     78    * Call by hander 
     79    * @access public 
     80    * @param $data  object cache data 
     81    * @param $id    stirng cache id 
     82    * @param $group array  Array of String. group name of this cache  
     83    * @return boolean  true (succeeded) / false (faild) 
     84    */ 
     85    public function save( $data, $id, $group = null ) 
     86    { 
     87        foreach( $this->resources as $idx => $res ){ 
     88                $ret[] = $this->resources[$idx]->save( $data, $id, $group ); 
     89        } 
     90        return !in_array( $ret, false, true ); 
     91    } 
     92    /**  
     93    * Call by hander 
     94    * @access public 
     95    * @param $id    stirng cache id 
     96    * @return boolean  true (succeeded) / false (faild) 
     97    */ 
     98    public function remove( $id ) 
     99    { 
     100        foreach( $this->resources as $idx => $res ){ 
     101                $ret[] = $this->resources[$idx]->remove( $id ); 
     102        } 
     103        return !in_array( $ret, false, true ); 
     104    } 
     105    /**  
     106    * Call by hander 
     107    * without group name, clean all chache 
     108    * @access public 
     109    * @param  String $group    stirng cache group name 
     110    * @param  Array  $ids array of id removed. 
     111    * @return boolean  true (succeeded) / false (faild) 
     112    */ 
     113    public function clean( $group =null, &$ids = null ) 
     114    { 
     115        foreach( $this->resources as $idx => $res ){ 
     116            $ret[] = $this->resources[$idx]->clean( $group, &$ids ); 
     117        } 
     118        return !in_array( $ret, false, true ); 
     119    } 
     120    /**  
     121    * Call by hander 
     122    * definition of get method 
     123    * @access public 
     124    * @param $id    stirng cache id 
     125    * @return string cached data, if cache does not exist, returns null 
     126    */ 
     127    public function get( $id ) 
     128    { 
     129        return $this->resources[$this->_target()]->get( $id ); 
     130    } 
     131    /**  
     132    * Call by hander. 
     133    * get Cache created time from this resource. 
     134    * @access public 
     135    * @param $id    stirng cache id 
     136    * @return string cache created time 
     137    */ 
     138    public function getLastModified( $id ) 
     139    { 
     140        return $this->resources[$this->_target()]->getLastModified( $id ); 
     141    } 
     142    /**  
     143    * Call by hander. 
     144    * Set or update  Cache created(modified) time. 
     145    * @access public 
     146    * @param $id    stirng cache id 
     147    * @param $date  stirng cache created time. default value date("r") 
     148    * @return String date of cache create or modified time 
     149    */ 
     150    public function setLastModified( $id, $date ) 
     151    { 
     152        foreach( $this->resources as $idx => $res ){ 
     153            $ret[] = $this->resources[$idx]->setLastModified( $id, $date ); 
     154        } 
     155        return !in_array( $ret, false, true ); 
     156    } 
     157    /**  
     158    * Call by hander. 
     159    * set or update cache TTL for checking expiration. 
     160    * @access public 
     161    * @param $id    stirng cache id 
     162    * @param $int   int    cache lifetime , default -1 
     163    * @return boolean  true (succeeded) / false (faild) 
     164    */ 
     165    public function setTimeToLive( $id, $int = null ) 
     166    { 
     167        foreach( $this->resources as $idx => $res ){ 
     168            $ret[] = $this->resources[$idx]->setTimeToLive( $id, $int ); 
     169        } 
     170        return !in_array( $ret, false, true ); 
     171    } 
     172    /**  
     173    * Call by hander. 
     174    * get cache Life time for checking expiration. 
     175    * @access public 
     176    * @param $id    stirng cache id 
     177    * @return int   cache lifetime. if value does not exits , this method return -1. 
     178    */ 
     179    public function getTimeToLive( $id ) 
     180    { 
     181        return $this->resources[$this->_target()]->getTimeToLive( $id ); 
     182    } 
     183    /**  
     184    * remove cache id ( arg $id ) from group index 
     185    * @access public 
     186    * @param  String $id cache id 
     187    * @return boolean  true (succeeded) / false (faild) 
     188    */ 
     189    public function removeFromGroup( $id, $group = null ) 
     190    { 
     191        foreach( $this->resources as $idx => $res ){ 
     192            $ret[] = $this->resources[$idx]->removeFromGroup( $id, $group); 
     193        } 
     194        return !in_array( $ret, false, true ); 
     195    } 
     196    /** 
     197    * Call by hander. 
     198    * definition of joinGroup method. 
     199    * Add $id to cache group $group 
     200    * @access public 
     201    * @param $id    stirng cache id 
     202    * @param $group stirng cache group name 
     203    * @return boolean  true (succeeded) / false (faild) 
     204    */ 
     205    public function addIntoGroup( $id, $group ) 
     206    { 
     207        foreach( $this->resources as $idx => $res ){ 
     208            $ret[] = $this->resources[$idx]->addIntoGroup( $id, $group ); 
     209        } 
     210        return !in_array( $ret, false, true ); 
     211    } 
    37212         
    38213} 
     
    76251// 
    77252?> 
     253