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 OR !($object instanceof $class)) {
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 $result = is_writeable(dirname($path)) ? @file_put_contents($path, $s, LOCK_EX) : false;
00146 if (!$result) @unlink($path);
00147 return $result;
00148 }
00149
00150 public function retrieve ($class, $time_limit = 0) {
00151
00152 $result = null;
00153 $path = $this->getCachePath($class);
00154 if (file_exists($path) AND ($string = @file_get_contents($path))) {
00155 $s = substr($string, 0, -32);
00156 $object = ($s AND (md5($s) == substr($string, -32))) ? unserialize($s) : null;
00157 if (is_object($object)) {
00158 $time_limit = $time_limit ? $time_limit : $this->timeout;
00159 $stamp = @$object->aliroCacheTimer;
00160 if ((time() - abs($stamp)) <= $time_limit) $result = $stamp > 0 ? $object : @$object->aliroCacheData;
00161 }
00162
00163 }
00164 return $result;
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 }
00198
00199 class aliroSingletonObjectCache extends aliroBasicCache {
00200 protected static $instance = __CLASS__;
00201 protected $timeout = _ALIRO_OBJECT_CACHE_TIME_LIMIT;
00202 protected $sizelimit = _ALIRO_OBJECT_CACHE_SIZE_LIMIT;
00203
00204 public static function getInstance () {
00205 return is_object(self::$instance) ? self::$instance : (self::$instance = new self::$instance);
00206 }
00207
00208 protected function getCachePath ($name) {
00209 return $this->basepath.'singleton/'.$name;
00210 }
00211
00212 public function delete () {
00213 $classes = func_get_args();
00214 foreach ($classes as $class) {
00215 $cachepath = $this->getCachePath($class);
00216 if (file_exists($cachepath)) unlink($cachepath);
00217 }
00218 }
00219
00220 public function deleteByExtension ($type) {
00221 $caches = array (
00222 'component' => 'aliroComponentHandler',
00223 'module' => 'aliroModuleHandler',
00224 'mambot' => 'aliroMambotHandler'
00225 );
00226 if (isset($caches[$type])) $this->delete($caches[$type]);
00227 }
00228
00229 }