| 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 | } |