| 7 | | class Cache_Handler{ |
| 8 | | public $resource; |
| 9 | | public static $default_resource; |
| 10 | | public function __construct($id, $group){ |
| 11 | | $this->setCacheId( $id ); |
| 12 | | $this->setCacheGroupName( $group ); |
| 13 | | $this->resource = self::$default_resource; |
| 14 | | } |
| 15 | | public static function setResource(/*Cache_ResourceInterface*/ $res ){ |
| 16 | | self::$default_resource = $res; |
| 17 | | } |
| 18 | | public static function clearCacheGroup( $name ){ |
| 19 | | self::$resource->clean( $name ); |
| 20 | | } |
| 21 | | /** |
| 22 | | * キャッシュを全て削除する |
| 23 | | */ |
| 24 | | public static function clearAll(){ |
| 25 | | self::$resource->clean(); |
| 26 | | } |
| 27 | | /** |
| 28 | | * プラグインを追加する |
| 29 | | * @access public |
| 30 | | */ |
| 31 | | public function addPlugin( Cache_CacheHandlerPlugIn $plugin ){ |
| 32 | | $this->plugin[] =$plugin; |
| 33 | | } |
| 34 | | /** |
| 35 | | * プラグインを削除する |
| 36 | | * @access public |
| 37 | | */ |
| 38 | | public function clearPlugin(){ |
| 39 | | unset( $this->plugin ); |
| 40 | | } |
| 41 | | /** |
| 42 | | * プラグインを削除する |
| 43 | | * @access public |
| 44 | | * @param int $index プラグインの順番 |
| 45 | | */ |
| 46 | | public function delPlugin( $index ){ |
| 47 | | unset( $this->plugin[$index] ); |
| 48 | | } |
| 49 | | /** |
| 50 | | * プラグインを取得する |
| 51 | | * @access public |
| 52 | | * @param int $index プラグインの順番 |
| 53 | | * @return Cache_CacheHandlerPlugIn プラグイン |
| 54 | | */ |
| 55 | | public function getPlugin($index){ |
| 56 | | return $this->plugin[$index]; |
| 57 | | } |
| 58 | | /** |
| 59 | | * プラグインを実行する |
| 60 | | */ |
| 61 | | private function beforeWrite( $data ){ |
| 62 | | if( $data == null ){ return; } |
| 63 | | foreach( $this->plugin as $modifer ){ |
| 64 | | $data = $modifer->beforeWrite( $data ); |
| 65 | | } |
| 66 | | return $data; |
| 67 | | } |
| 68 | | /** |
| 69 | | * プラグインを実行する |
| 70 | | */ |
| 71 | | private function afterRead( $data ){ |
| 72 | | if( $data == null ){ return; } |
| 73 | | foreach( array_reverse( $this->plugin ) as $modifer ){ |
| 74 | | $data = $modifer->afterRead( $data ); |
| 75 | | } |
| 76 | | return $data; |
| 77 | | } |
| 78 | | |
| 79 | | /** |
| 80 | | * このインスタンスが対応するキャッシュを削除する |
| 81 | | * @access public |
| 82 | | * @param void |
| 83 | | * @return void |
| 84 | | */ |
| 85 | | public function clear(){ |
| 86 | | $this->resource->remove( $this->cache_id ); |
| 87 | | } |
| 88 | | /** |
| 89 | | * キャッシュをチェックする。有効期限内なら、trueを返す。 |
| 90 | | * 有効期限が切れているならキャッシュを削除してfalseを返す |
| 91 | | * @access public |
| 92 | | * @param String $date 有効期限 日付 |
| 93 | | * @return boolean |
| 94 | | */ |
| 95 | | public function check( $date ){ |
| 96 | | if( $this->isCached( $date ) ){ |
| 97 | | return true; |
| 98 | | }else{ |
| 99 | | $this->clear(); |
| 100 | | return false; |
| 101 | | } |
| 102 | | } |
| 103 | | /** |
| 104 | | * このインスタンスがキャッシュを保持してるかどうか |
| 105 | | * また、キャッシュが有効期限内かどうか |
| 106 | | * @access public |
| 107 | | * @param String $date 有効期限日付 |
| 108 | | * @return boolean |
| 109 | | */ |
| 110 | | public function isCached( $date = null ){ |
| 111 | | if( $this->getCache() == null || $this->getCache() == "" ){ |
| 112 | | return false; |
| 113 | | }else if( $date == null ){ |
| 114 | | return true; |
| 115 | | }else if( strtotime( $date ) == -1 ){ |
| 116 | | return false; |
| 117 | | }else{ |
| 118 | | return strtotime($date) <= strtotime($this->getLastModified()); |
| 119 | | } |
| 120 | | } |
| 121 | | /** |
| 122 | | * キャッシュを取得する |
| 123 | | * @access public |
| 124 | | * @param void |
| 125 | | * @return String キャッシュ |
| 126 | | */ |
| 127 | | public function getCache(){ |
| 128 | | $data = $this->resource->get( $this->cache_id ); |
| 129 | | $data = $this->afterRead( $data ); |
| 130 | | return $data; |
| 131 | | } |
| 132 | | /** |
| 133 | | * このインスタンスのキャッシュの作成日時を返す |
| 134 | | * @return String date("r") |
| 135 | | */ |
| 136 | | public function getLastModified(){ |
| 137 | | if( $this->isCached() == false ){ |
| 138 | | return null; |
| 139 | | } |
| 140 | | return $this->resource->getLastModified( $this->cache_id ); |
| 141 | | } |
| 142 | | /** |
| 143 | | * このインスタンスのキャッシュ名をセットする |
| 144 | | * @access public |
| 145 | | * @param String $id キャッシュ名 |
| 146 | | * @return void |
| 147 | | */ |
| 148 | | public function setCacheId( $id ){ |
| 149 | | $this->cache_id = $id; |
| 150 | | } |
| 151 | | /** |
| 152 | | * キャッシュ名を取得する |
| 153 | | * @access public |
| 154 | | * @param void |
| 155 | | * @return String キャッシュ名 |
| 156 | | */ |
| 157 | | public function getCacheId(){ |
| 158 | | return $this->cache_id; |
| 159 | | } |
| 160 | | /** |
| 161 | | * このインスタンスのキャッシュグループ名をセットする |
| 162 | | * @access public |
| 163 | | * @param String $name |
| 164 | | * @return void |
| 165 | | */ |
| 166 | | public function addCacheGroupName( $name ){ |
| 167 | | $this->group[] = $name; |
| 168 | | } |
| 169 | | public function setCacheGroupName( $names ){ |
| 170 | | if( is_array( $names ) ){ |
| 171 | | $this->group = $names; |
| 172 | | }else{ |
| 173 | | $this->addCacheGroupName( $name ); |
| 174 | | } |
| 175 | | } |
| 176 | | /** |
| 177 | | * キャッシュを保存する |
| 178 | | * @access public |
| 179 | | * @param String キャッシュする内容 |
| 180 | | * @return void |
| 181 | | */ |
| 182 | | public function setCache( $data ){ |
| 183 | | $data = $this->beforeWrite( $data ); |
| 184 | | $this->resource->save( $data, $this->cache_id, $this->group ); |
| 185 | | } |
| 186 | | public function setLastModified( $date ){ |
| 187 | | $this->resource->setLastModified( $this->cache_id, $date ); |
| 188 | | } |
| | 16 | class Cache_Handler { |
| | 17 | |
| | 18 | /** |
| | 19 | * Cache resource |
| | 20 | * @access public |
| | 21 | * @type Cache_Handler_Resource_Interface |
| | 22 | */ |
| | 23 | public static $resource; |
| | 24 | /** |
| | 25 | * constructor |
| | 26 | * @access public |
| | 27 | * @param array $ini array( "property name" => "value " ); |
| | 28 | * @return void |
| | 29 | */ |
| | 30 | public function __construct($id, $group) |
| | 31 | { |
| | 32 | $this->setCacheId( $id ); |
| | 33 | $this->setCacheGroupName( $group ); |
| | 34 | $this->resource =& self::$resource; |
| | 35 | $this->plugin = array(); |
| | 36 | } |
| | 37 | /** |
| | 38 | * set Cache resource to this cache handler as static |
| | 39 | * @access public |
| | 40 | * @type Cache_Handler_Resource_Interface |
| | 41 | * @return void |
| | 42 | */ |
| | 43 | public static function setResource( Cache_Handler_Resource_Interface $res ) |
| | 44 | { |
| | 45 | self::$resource = $res; |
| | 46 | } |
| | 47 | /** |
| | 48 | * get Cache resource to this cache handler as static |
| | 49 | * @access public |
| | 50 | * @return Cache_Handler_Resource_Interface |
| | 51 | */ |
| | 52 | public static function getResource() |
| | 53 | { |
| | 54 | return self::$resource; |
| | 55 | } |
| | 56 | /** |
| | 57 | * Clear Cache group from this resource |
| | 58 | * @access public |
| | 59 | * @param String group name |
| | 60 | * @return boolean true (succeeded) / false (faild) |
| | 61 | */ |
| | 62 | public static function clearCacheGroup( $name ) |
| | 63 | { |
| | 64 | return self::$resource->clean( $name ); |
| | 65 | } |
| | 66 | /** |
| | 67 | * clear all cache from this resource |
| | 68 | * @return boolean true (succeeded) / false (faild) |
| | 69 | */ |
| | 70 | public static function clearAll() |
| | 71 | { |
| | 72 | self::$resource->clean(); |
| | 73 | } |
| | 74 | /** |
| | 75 | * Add plugin to this class |
| | 76 | * plugin is executed by added order |
| | 77 | * @access public |
| | 78 | */ |
| | 79 | public function addPlugin( Cache_Handler_PlugIn_Interface $plugin ) |
| | 80 | { |
| | 81 | $this->plugin[] =$plugin; |
| | 82 | } |
| | 83 | /** |
| | 84 | * clear all registed plugin. |
| | 85 | * @access public |
| | 86 | */ |
| | 87 | public function clearPlugin() |
| | 88 | { |
| | 89 | unset( $this->plugin ); |
| | 90 | } |
| | 91 | /** |
| | 92 | * Unregist plugin |
| | 93 | * @access public |
| | 94 | * @param int $index order of plugin |
| | 95 | */ |
| | 96 | public function delPlugin( $index ) |
| | 97 | { |
| | 98 | unset( $this->plugin[$index] ); |
| | 99 | } |
| | 100 | /** |
| | 101 | * Retrieve registered plugin |
| | 102 | * @access public |
| | 103 | * @param int $index order of plugin |
| | 104 | * @return Cache_Handler_PlugIn_Interface plugin |
| | 105 | */ |
| | 106 | public function & getPlugin($index) |
| | 107 | { |
| | 108 | return $this->plugin[$index]; |
| | 109 | } |
| | 110 | /** |
| | 111 | * execute plugin |
| | 112 | * @access protected |
| | 113 | */ |
| | 114 | protected function beforeWrite( $data ) |
| | 115 | { |
| | 116 | if( $data == null ){ |
| | 117 | return; |
| | 118 | } |
| | 119 | foreach( $this->plugin as $modifer ){ |
| | 120 | $data = $modifer->beforeWrite( $data ); |
| | 121 | } |
| | 122 | return $data; |
| | 123 | } |
| | 124 | /** |
| | 125 | * execute plugin |
| | 126 | * @access protected |
| | 127 | */ |
| | 128 | protected function afterRead( $data ) |
| | 129 | { |
| | 130 | if( $data == null ){ |
| | 131 | return; |
| | 132 | } |
| | 133 | foreach( array_reverse( $this->plugin ) as $modifer ){ |
| | 134 | $data = $modifer->afterRead( $data ); |
| | 135 | } |
| | 136 | return $data; |
| | 137 | } |
| | 138 | |
| | 139 | /** |
| | 140 | * clear cache of this cache_id |
| | 141 | * @access public |
| | 142 | * @param void |
| | 143 | * @return void |
| | 144 | */ |
| | 145 | public function clear() |
| | 146 | { |
| | 147 | $this->resource->remove( $this->cache_id ); |
| | 148 | } |
| | 149 | /** |
| | 150 | * check is this cache_id cached in resource. |
| | 151 | * @access private |
| | 152 | * @return boolean |
| | 153 | */ |
| | 154 | public function isCached( $date = null ) |
| | 155 | { |
| | 156 | if( $this->_data == null ){ |
| | 157 | $this->_data = $this->getCache(); |
| | 158 | } |
| | 159 | |
| | 160 | if( $this->_data === FALSE || $this->_data === null || $this->_data == ""){ |
| | 161 | return false; |
| | 162 | }else{ |
| | 163 | return true; |
| | 164 | } |
| | 165 | } |
| | 166 | /** |
| | 167 | * Check cache is not expired. |
| | 168 | * If cache life time is expired, clear cache and return false. |
| | 169 | * @access public |
| | 170 | * @param String $time, cache life time |
| | 171 | * @return boolean |
| | 172 | */ |
| | 173 | public function check() |
| | 174 | { |
| | 175 | if( $this->isCached( $date ) ){ |
| | 176 | return true; |
| | 177 | }else{ |
| | 178 | if( $this->isExpired() ){ |
| | 179 | $this->clear(); |
| | 180 | } |
| | 181 | return false; |
| | 182 | } |
| | 183 | } |
| | 184 | /** |
| | 185 | * return true if cache is expired |
| | 186 | */ |
| | 187 | public function isExpired() |
| | 188 | { |
| | 189 | $modifed = $this->getLastModified(); |
| | 190 | $lifetime = $this->getCacheLifeTime(); |
| | 191 | if( $lifetime == null ){//eternal cache |
| | 192 | return false; |
| | 193 | }else{ |
| | 194 | return time() >= strtotime( $modified )+$lifetime; |
| | 195 | } |
| | 196 | } |
| | 197 | /** |
| | 198 | * return true if cache is NOT expired |
| | 199 | */ |
| | 200 | public function isNotExpired() |
| | 201 | { |
| | 202 | return !$this->isExpired(); |
| | 203 | } |
| | 204 | /** |
| | 205 | * get cache of this cache id, and plugin applied |
| | 206 | * @access public |
| | 207 | * @param void |
| | 208 | * @return String cache data |
| | 209 | */ |
| | 210 | public function getCache() |
| | 211 | { |
| | 212 | $data = $this->resource->get( $this->cache_id ); |
| | 213 | $data = $this->afterRead( $data ); |
| | 214 | return $data; |
| | 215 | } |
| | 216 | /** |
| | 217 | * Return cached modified date of this cache id. |
| | 218 | * When resource has no cache, this function returns null. |
| | 219 | * @return String date |
| | 220 | */ |
| | 221 | public function getLastModified() |
| | 222 | { |
| | 223 | if( $this->isCached() == false ){ |
| | 224 | return null; |
| | 225 | } |
| | 226 | return $this->resource->getLastModified( $this->cache_id ); |
| | 227 | } |
| | 228 | /** |
| | 229 | * return cached life time of this cache id. |
| | 230 | * when resource has no cache, this function returns null. |
| | 231 | * @return int |
| | 232 | */ |
| | 233 | public function getCacheLifeTime() |
| | 234 | { |
| | 235 | if( $this->isCached() == false ){ |
| | 236 | return null; |
| | 237 | } |
| | 238 | return $this->resource->getTimeToLive( $this->cache_id ); |
| | 239 | } |
| | 240 | /** |
| | 241 | * set this cache id |
| | 242 | * @access public |
| | 243 | * @param String $id cache id |
| | 244 | * @return void |
| | 245 | */ |
| | 246 | public function setCacheId( $id ) |
| | 247 | { |
| | 248 | $this->cache_id = $id; |
| | 249 | } |
| | 250 | /** |
| | 251 | * returns cache id |
| | 252 | * @access public |
| | 253 | * @param void |
| | 254 | * @return String cache id |
| | 255 | */ |
| | 256 | public function getCacheId() |
| | 257 | { |
| | 258 | return $this->cache_id; |
| | 259 | } |
| | 260 | /** |
| | 261 | * add cache group name to this cache id |
| | 262 | * @access public |
| | 263 | * @param String $name |
| | 264 | * @return void |
| | 265 | */ |
| | 266 | public function addCacheGroupName( $name ) |
| | 267 | { |
| | 268 | if( $name != "" ){ |
| | 269 | $ret = $this->resource->addIntoGroup( $this->cache_id, $name ); |
| | 270 | $this->group[] = $name; |
| | 271 | return true; |
| | 272 | } |
| | 273 | return false; |
| | 274 | } |
| | 275 | |
| | 276 | /** |
| | 277 | * resets cache group name already set, and set cache group name. |
| | 278 | * @access |
| | 279 | * @param |
| | 280 | * @return |
| | 281 | */ |
| | 282 | public function setCacheGroupName( $names ) |
| | 283 | { |
| | 284 | if( is_array( $names ) ){ |
| | 285 | $this->group = $names; |
| | 286 | }else{ |
| | 287 | $this->addCacheGroupName( $name ); |
| | 288 | } |
| | 289 | return true; |
| | 290 | } |
| | 291 | /** |
| | 292 | * store cache data to this resource |
| | 293 | * @access public |
| | 294 | * @param String content wanted to be cached |
| | 295 | * @return void |
| | 296 | */ |
| | 297 | public function setCache( $data ) |
| | 298 | { |
| | 299 | $data = $this->beforeWrite( $data ); |
| | 300 | return $this->resource->save( $data, $this->cache_id, $this->group ); |
| | 301 | } |
| | 302 | /** |
| | 303 | * set or modify cache last modified date |
| | 304 | * @access public |
| | 305 | * @param String $date , if this param is skipped date("r") will be set to |
| | 306 | * @return boolean |
| | 307 | */ |
| | 308 | public function setLastModified( $date = null ) |
| | 309 | { |
| | 310 | if( $date == null ){ |
| | 311 | $date = date("r"); |
| | 312 | } |
| | 313 | return $this->resource->setLastModified( $this->cache_id, $date ); |
| | 314 | } |
| | 315 | /** |
| | 316 | * set or modify cache last modified date |
| | 317 | * @access public |
| | 318 | * @param String $date , default 3600, |
| | 319 | * 'setCacheLifeTime( NULL )' or 'setCacheLifeTime("")' is for eternal cache. |
| | 320 | * @return boolean |
| | 321 | */ |
| | 322 | public function setCacheLifeTime( $time = 3600 ) |
| | 323 | { |
| | 324 | if( $time == null || $time == "" ){ |
| | 325 | $time == ""; |
| | 326 | } |
| | 327 | return $this->resource->setTimeToLive( $this->cache_id, $time ); |
| | 328 | } |