00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 class aliroProfiler {
00061 private $start=0;
00062 private $prefix='';
00063
00064 function __construct ( $prefix='' ) {
00065 $this->start = $this->getmicrotime();
00066 $this->prefix = $prefix;
00067 }
00068
00069 public function reset () {
00070 $this->start = $this->getmicrotime();
00071 }
00072
00073 public function mark( $label ) {
00074 return sprintf ( "\n$this->prefix %.3f $label", $this->getmicrotime() - $this->start );
00075 }
00076
00077 public function getElapsed () {
00078 return $this->getmicrotime() - $this->start;
00079 }
00080
00081 private function getmicrotime(){
00082 list($usec, $sec) = explode(" ",microtime());
00083 return ((float)$usec + (float)$sec);
00084 }
00085 }
00086
00087 abstract class cachedSingleton {
00088
00089 protected function __clone () { }
00090
00091 protected static function getCachedSingleton ($class) {
00092 $objectcache = aliroSingletonObjectCache::getInstance();
00093 $object = $objectcache->retrieve($class);
00094 if ($object == null) {
00095 $object = new $class();
00096 $objectcache->store($object);
00097 }
00098 return $object;
00099 }
00100
00101 public function clearCache ($immediate=false) {
00102 $objectcache = aliroSingletonObjectCache::getInstance();
00103 $classname = get_class($this);
00104 $objectcache->delete($classname);
00105 if ($immediate) {
00106 $instancevar = $classname.'::$instance';
00107 eval("$instancevar = '$classname';");
00108 }
00109 }
00110
00111 public function cacheNow () {
00112 aliroSingletonObjectCache::getInstance()->store($this);
00113 }
00114
00115 }
00116
00117 abstract class aliroBasicCache {
00118 private static $mops = array();
00119 protected $basepath = '';
00120 protected $sizelimit = 0;
00121 protected $timeout = 0;
00122
00123 public function __construct () {
00124 $this->basepath = _ALIRO_CLASS_BASE.'/cache/';
00125 }
00126
00127 public function __destruct () {
00128 foreach (self::$mops as $mop) if ($mop) shmop_close($mop);
00129 }
00130
00131 abstract protected function getCachePath ($name);
00132
00133 public function store ($object, $cachename='') {
00134 $path = $this->getCachePath($cachename ? $cachename : get_class($object));
00135 if (is_object($object)) $object->aliroCacheTimer = time();
00136 else {
00137 $givendata = $object;
00138 $object = new stdClass();
00139 $object->aliroCacheData = $givendata;
00140 $object->aliroCacheTimer = -time();
00141 }
00142 $s = serialize($object);
00143 $s .= md5($s);
00144 if (strlen($s) > $this->sizelimit) return false;
00145 return is_writeable(dirname($path)) ? file_put_contents($path, $s, LOCK_EX) : false;
00146 }
00147
00148 public function retrieve ($class, $time_limit = 0) {
00149
00150 $result = null;
00151 $path = $this->getCachePath($class);
00152 if (file_exists($path) AND ($string = @file_get_contents($path))) {
00153 $s = substr($string, 0, -32);
00154 $object = ($s AND (md5($s) == substr($string, -32))) ? unserialize($s) : null;
00155 if (is_object($object)) {
00156 $time_limit = $time_limit ? $time_limit : $this->timeout;
00157 $stamp = @$object->aliroCacheTimer;
00158 if ((time() - abs($stamp)) <= $time_limit) $result = $stamp > 0 ? $object : @$object->aliroCacheData;
00159 }
00160
00161 }
00162 return $result;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 }
00196
00197 class aliroSingletonObjectCache extends aliroBasicCache {
00198 protected static $instance = __CLASS__;
00199 protected $timeout = _ALIRO_OBJECT_CACHE_TIME_LIMIT;
00200 protected $sizelimit = _ALIRO_OBJECT_CACHE_SIZE_LIMIT;
00201
00202 public static function getInstance () {
00203 return is_object(self::$instance) ? self::$instance : (self::$instance = new self::$instance);
00204 }
00205
00206 protected function getCachePath ($name) {
00207 return $this->basepath.'singleton/'.$name;
00208 }
00209
00210 public function delete () {
00211 $classes = func_get_args();
00212 foreach ($classes as $class) {
00213 $cachepath = $this->getCachePath($class);
00214 if (file_exists($cachepath)) unlink($cachepath);
00215 }
00216 }
00217
00218 public function deleteByExtension ($type) {
00219 $caches = array (
00220 'component' => 'aliroComponentHandler',
00221 'module' => 'aliroModuleHandler',
00222 'mambot' => 'aliroMambotHandler'
00223 );
00224 if (isset($caches[$type])) $this->delete($caches[$type]);
00225 }
00226
00227 }