# DebugCachePeer.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2009 by Denis M. Gabaidulin                             *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00017     final class DebugCachePeer extends SelectivePeer
    00018     {
    00019         private $peer               = null;
    00020         private $logger             = null;
    00021         private $isWeb              = true;
    00022         private $whiteListActions   = array();
    00023         private $blackListActions   = array();
    00024         private $actionFilter       = false;
    00025
    00026
    00030         public static function create(
    00031             CachePeer $peer, $logfile, $isWeb = true, $appendFile = true
    00032         )
    00033         {
    00034             return new self($peer, $logfile, $isWeb, $appendFile);
    00035         }
    00036
    00037         public function __construct(
    00038             CachePeer $peer, $logfile, $isWeb = true, $appendFile = true
    00039         )
    00040         {
    00041             $this->peer     = $peer;
    00042             $this->isWeb    = $isWeb;
    00043             $this->logger   =
    00044                 StreamLogger::create()->
    00045                 setOutputStream(FileOutputStream::create($logfile, $appendFile));
    00046         }
    00047
    00048         public function setBlackListActions($actions)
    00049         {
    00050             if (!empty($this->whiteListActions))
    00051                 throw new WrongStateException('You already setup black list!');
    00052
    00053             $this->blackListActions = $actions;
    00054
    00055             $this->actionFilter = true;
    00056
    00057             return $this;
    00058         }
    00059
    00060         public function dropBlackListActions()
    00061         {
    00062             $this->blackListActions = array();
    00063
    00064             $this->actionFilter = false;
    00065
    00066             return $this;
    00067         }
    00068
    00069         public function setWhiteListActions($actions)
    00070         {
    00071             if (!empty($this->blackListActions))
    00072                 throw new WrongStateException('You already setup white list!');
    00073
    00074             $this->whiteListActions = $actions;
    00075
    00076             $this->actionFilter = true;
    00077
    00078             return  $this;
    00079         }
    00080
    00081         public function dropWhiteListActions()
    00082         {
    00083             $this->whiteListActions = array();
    00084
    00085             $this->actionFilter = false;
    00086
    00087             return $this;
    00088         }
    00089
    00093         public function mark($className)
    00094         {
    00095             return $this;
    00096         }
    00097
    00098         public function increment($key, $value)
    00099         {
    00100             $beginTime  = microtime(true);
    00101             $value      = $this->peer->increment($key, $value);
    00102             $totalTime  =  microtime(true) - $beginTime;
    00103
    00104             $this->log('increment', $totalTime, $key);
    00105
    00106             return $value;
    00107         }
    00108
    00109         public function decrement($key, $value)
    00110         {
    00111             $beginTime  = microtime(true);
    00112             $value      = $this->peer->decrement($key, $value);
    00113             $totalTime  =  microtime(true) - $beginTime;
    00114
    00115             $this->log('decrement', $totalTime, $key);
    00116
    00117             return $value;
    00118         }
    00119
    00120         public function getList($indexes)
    00121         {
    00122             $beginTime  = microtime(true);
    00123             $value      = $this->peer->getList($indexes);
    00124             $totalTime  =  microtime(true) - $beginTime;
    00125
    00126             $this->log('getList', $totalTime, implode(',', $indexes));
    00127
    00128             return $value;
    00129         }
    00130
    00131         public function get($key)
    00132         {
    00133             $beginTime  = microtime(true);
    00134             $value      = $this->peer->get($key);
    00135             $totalTime  =  microtime(true) - $beginTime;
    00136
    00137             $this->log('get', $totalTime, $key);
    00138
    00139             return $value;
    00140         }
    00141
    00142         public function delete($key)
    00143         {
    00144             $beginTime  = microtime(true);
    00145             $value      = $this->peer->delete($key);
    00146             $totalTime  =  microtime(true) - $beginTime;
    00147
    00148             $this->log('delete', $totalTime, $key);
    00149
    00150             return $value;
    00151         }
    00152
    00156         public function clean()
    00157         {
    00158             $beginTime  = microtime(true);
    00159             $value      = $this->peer->clean();
    00160             $totalTime  =  microtime(true) - $beginTime;
    00161
    00162             $this->log('clean', $totalTime);
    00163
    00164             return $value;
    00165         }
    00166
    00167         public function isAlive()
    00168         {
    00169             $beginTime  = microtime(true);
    00170             $value      = $this->peer->isAlive();
    00171             $totalTime  =  microtime(true) - $beginTime;
    00172
    00173             $this->log('isAlive', $totalTime);
    00174
    00175             return $value;
    00176         }
    00177
    00178         public function append($key, $data)
    00179         {
    00180             $beginTime  = microtime(true);
    00181             $value      = $this->peer->append($key, $data);
    00182             $totalTime  =  microtime(true) - $beginTime;
    00183
    00184             $this->log('append', $totalTime, $key);
    00185
    00186             return $value;
    00187         }
    00188
    00189         protected function store(
    00190             $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM
    00191         )
    00192         {
    00193             $beginTime  = microtime(true);
    00194             $value      = $this->peer->store($action, $key, $value, $expires);
    00195             $totalTime  =  microtime(true) - $beginTime;
    00196
    00197             $this->log('store + '.$action, $totalTime, $key);
    00198
    00199             return $value;
    00200         }
    00201
    00202         private function log($action, $totalTime, $key = null)
    00203         {
    00204             if ($this->actionFilter) {
    00205                 if (
    00206                     !empty($this->blackListActions)
    00207                     && in_array($action, $this->blackListActions)
    00208                 )
    00209                     return $this;
    00210
    00211                 if (
    00212                     !empty($this->whiteListActions)
    00213                     && !in_array($action, $this->whiteListActions)
    00214                 )
    00215                     return $this;
    00216             }
    00217
    00218             $record = null;
    00219
    00220             if ($this->isWeb)
    00221                 $record .= (
    00222                     (isset($_SERVER['SSI_REQUEST_URI']))
    00223                         ? $_SERVER['SSI_REQUEST_URI']
    00224                         : $_SERVER['REQUEST_URI']
    00225                 )."\t";
    00226
    00227             $record .= $action."\t".$key."\t".$totalTime;
    00228
    00229             $this->logger->info($record);
    00230
    00231             return $this;
    00232         }
    00233     }
    00234 ?>