Exemplo n.º 1
0
 public function load(Reader $reader)
 {
     $caches = $reader->read();
     foreach ($caches as $cacheName => $cacheValue) {
         // Check name
         if (!Validate::isVariableName($cacheName)) {
             throw new \Exception('Name of cache must be a valid variable name');
         }
         // Check options
         $params = array();
         foreach ($cacheValue as $name => $value) {
             // no use comment (for xml file)
             if ($name == 'comment') {
                 continue;
             }
             // cast
             if (is_string($value)) {
                 $value = Tools::castValue($value);
             }
             // add value into cache parameters
             $params[$name] = $value;
         }
         // check adaptater
         if (!isset($params['adaptater'])) {
             throw new \Exception('Miss adaptater parameter for cache : "' . $cacheName . '"');
         }
         // Add param name
         $params['name'] = $cacheName;
         // Add cache
         CacheManager::addCache($cacheName, CacheManager::factory($params['adaptater'], $params, 'framework\\cache\\adaptaters', 'framework\\cache\\IAdaptater'), true);
     }
 }
Exemplo n.º 2
0
 public static function setCache($cacheName)
 {
     $cache = Cache::getCache($cacheName);
     if (!$cache) {
         throw new \Exception('Invalid cache');
     }
     self::$_cache = $cache;
 }
Exemplo n.º 3
0
 public function setCache($cacheName)
 {
     $cache = Cache::getCache($cacheName);
     if (!$cache) {
         throw new \Exception('Cache : "' . $cacheName . '" is not a valid cache');
     }
     $this->_cache = $cache;
     return $this;
 }
Exemplo n.º 4
0
 public function __construct()
 {
     //check lang is in url and auto set
     if ($this->router->getCurrentRoute() == 'index' && stripos(Http::getCurrentUrl(), $this->language->getLanguage()) === false) {
         Http::redirect($this->router->getUrl('index'));
     }
     $this->tpl->setVar('langAvaible', $this->language->getLanguage() == 'fr_FR' ? 'en_EN' : 'fr_FR');
     //cache
     $this->_cache = Cache::getCache('bdd');
     $this->tpl->setVar('isConnected', Members::isConnected());
 }
Exemplo n.º 5
0
 public function __construct($params = array())
 {
     parent::init($params);
     Logger::getInstance()->addGroup('cache' . $this->_name, 'Cache ' . $this->_name, true, true);
     if (!isset($params['path']) || !is_string($params['path'])) {
         throw new \Exception('Miss path parameter, or is invalid');
     }
     if (!is_dir($params['path'])) {
         if (!mkdir($params['path'], 0775, true)) {
             throw new \Exception('Error on creating "' . $params['path'] . '" directory');
         }
     }
     if (!is_writable($params['path'])) {
         throw new \Exception('Directory "' . $params['path'] . '" is not writable');
     }
     $this->_path = realpath($params['path']) . DS;
 }
Exemplo n.º 6
0
 /**
  * Checks whether an update is available.
  */
 public function resolve(Request $request, Response $response)
 {
     $path = $request->uri('path');
     $hash = $request->param('v');
     $info = Cache::getInfo($path, $hash);
     if (!$info) {
         return;
     }
     // Send a bunch of headers
     // 1. Conditional request
     // If-Modified-Since: Match against $info->getMTime();
     // If-None-Match: Match against the md5_file();
     // 2. Normal request
     // Content-Type + charset
     // Content-Length
     // Cache-Control
     // Date
     // Pragma (remove)
     // header('Content-Type: application/json; charset=utf-8', true);
 }
Exemplo n.º 7
0
 public function __construct($params = array())
 {
     if (!extension_loaded('apcu') && !extension_loaded('apc')) {
         throw new \Exception('Apcu extension not loaded try change your PHP configuration');
     }
     parent::init($params);
     Logger::getInstance()->addGroup('cache' . $this->_name, 'Cache ' . $this->_name, true, true);
     //create dynamic key and add to prefix (it's for multi applications)
     if (!file_exists(PATH_CACHE . $this->_name)) {
         $key = rand();
         $file = new \SplFileObject(PATH_CACHE . 'ApcuKey' . $this->_name, 'w+');
         if ($file->flock(LOCK_EX)) {
             $file->fwrite($key);
             $file->flock(LOCK_UN);
         }
         $this->_prefix .= $key;
     } else {
         $this->_prefix .= file_get_contents(PATH_CACHE . $this->_name);
     }
 }
Exemplo n.º 8
0
 public function stop()
 {
     if ($this->_isInit && $this->_isRun) {
         // run caches gc
         $caches = Cache::getCaches();
         foreach ($caches as $cache) {
             $cache->runGc();
         }
         //profiling
         if (self::getProfiler()) {
             // Caches
             foreach ($caches as $cache) {
                 Logger::getInstance()->debug('Adaptater : "' . get_class($cache) . '"', 'cache' . $cache->getName());
             }
             // Databases
             $databases = Database::getDatabases();
             foreach ($databases as $database) {
                 Logger::getInstance()->debug('Type : ' . $database->getType(), 'database' . $database->getName());
                 Logger::getInstance()->debug('Adaptater : ' . get_class($database->getAdaptater()), 'database' . $database->getName());
                 $stats = $database->getStats();
                 Logger::getInstance()->debug('Queries : ' . (string) $database->getQueryCount() . ' (Aproximately memory used  : ' . $stats['ram'] . ' KB in aproximately ' . $stats['time'] . ' ms)', 'database' . $database->getName());
                 Logger::getInstance()->debug('Servers : ' . $database->countServers() . ' (Masters : ' . $database->countServers(Server::TYPE_MASTER) . '  Slaves : ' . $database->countServers(Server::TYPE_SLAVE) . ')', 'database' . $database->getName());
             }
             // Templates
             $templates = Template::getTemplates();
             foreach ($templates as $template) {
                 Logger::getInstance()->debug('Adaptater : ' . get_class($template), 'template' . $template->getName());
             }
             // Language
             Logger::getInstance()->debug('Language default is : "' . Language::getInstance()->getDefaultLanguage() . '"', 'language');
             Logger::getInstance()->debug(Language::getInstance()->countVars() . ' vars defined', 'language');
             // Router
             Logger::getInstance()->debug('Current url : ' . Http::getCurrentUrl(), 'router');
             Logger::getInstance()->debug('Current route : ' . Router::getInstance()->getCurrentRoute(), 'router');
             Logger::getInstance()->debug('Current route rule : ' . Router::getInstance()->getCurrentRule(), 'router');
             Logger::getInstance()->debug('Ajax request : ' . (int) Http::isAjax(), 'router');
             Logger::getInstance()->debug('Ssl request : ' . (int) Http::isHttps(), 'router');
             Logger::getInstance()->debug('Request dispatched in aproximately : ' . Benchmark::getInstance('router')->stopTime()->getStatsTime() . ' ms', 'router');
             Logger::getInstance()->debug('Aproximately memory used  : ' . Benchmark::getInstance('router')->stopRam()->getStatsRam() . ' KB', 'router');
             // Logger debug informations and benchmark
             Logger::getInstance()->addGroup('logger', 'Logger Benchmark and Informations', true);
             Logger::getInstance()->debug(Logger::getInstance()->countObservers() . ' observers registered', 'logger');
             Logger::getInstance()->debug(Logger::getInstance()->countGroups() . ' groups and ' . (Logger::getInstance()->countLogs() + 3) . ' logs', 'logger');
             Logger::getInstance()->debug('In aproximately ' . Benchmark::getInstance('logger')->stopTime()->getStatsTime() . ' ms', 'logger');
             Logger::getInstance()->debug('Aproximately memory used  : ' . Benchmark::getInstance('logger')->stopRam()->getStatsRam() . ' KB', 'logger');
             // Autoloader
             Logger::getInstance()->addGroup('autoloader', 'Autoloader report', true);
             $logs = Autoloader::getLogs();
             foreach ($logs as &$log) {
                 Logger::getInstance()->debug($log, 'autoloader');
             }
             Logger::getInstance()->debug(count(Autoloader::getAutoloaders()) . ' autoloader adaptaters, ' . count(Autoloader::getDirectories()) . ' directories and ' . count(Autoloader::getNamespaces()) . ' namespaces registered', 'autoloader');
             Logger::getInstance()->debug('Loading ' . count(Autoloader::getClasses()) . ' classes (' . Autoloader::countGlobalizedClasses() . ' globalized classes)  in aproximately ' . round(Autoloader::getBenchmark('time') * 1000, 4) . ' ms', 'autoloader');
             Logger::getInstance()->debug('Aproximately memory used  : ' . round(Autoloader::getBenchmark('memory') / 1024, 4) . ' KB', 'autoloader');
             Autoloader::purgeLogs();
             Autoloader::purgeBenchmark();
             // Global informations && Benchmark
             Logger::getInstance()->addGroup('global', 'Global Benchmark and Informations', true);
             Logger::getInstance()->debug('Page generated in aproximately : ' . Benchmark::getInstance('global')->stopTime()->getStatsTime() . ' ms', 'global');
             Logger::getInstance()->debug('Aproximately memory used  : ' . Benchmark::getInstance('global')->stopRam()->getStatsRam() . ' KB - Memory allocated : ' . memory_get_peak_usage(true) / 1024 . ' KB', 'global');
         }
         //notify logger
         Logger::getInstance()->notify();
         // Stop managers
         ExceptionManager::getInstance()->stop();
         ErrorManager::getInstance()->stop();
         // avoid multi call
         $this->_isInit = false;
         $this->_isRun = false;
     }
 }
Exemplo n.º 9
0
 public function resolve(Request $request, Response $response)
 {
     $path = $this->srcPath . $request->uri('path') . '.url';
     // Check if target file is a proxy.
     if (!is_file($path)) {
         return;
     }
     $cacheTarget = parse_ini_file($path);
     $cacheTarget = @$cacheTarget['URL'];
     unset($path);
     if (!$cacheTarget) {
         Log::warning('Proxy file has not URL parameter.', array('requestUri' => $request->uri(), 'proxyFile' => $request->uri('path') . '.uri'));
         $response->status(502);
         // Bad Gateway
         return;
     }
     /*! Cache Header Notes
      *
      *  # Cache-Control
      *  [public | private] Cacheable when public, otherwise the client is responsible for caching.
      *  [no-cache( \w+)?]  When no fields are specified, the whole thing must revalidate everytime,
      *                     otherwise cache it except specified fields.
      *  [no-store] 				 Ignore caching and pipe into output.
      *  [max-age=\d+] 		 Seconds before this cache is meant to expire, this overrides Expires header.
      *  [s-maxage=\d+] 		 Overrides max-age and Expires header, behaves just like max-age.
      *                 	   (This is for CDN and we are using it.)
      *  [must-revalidate]  Tells those CDNs which are intended to serve stale contents to revalidate every time.
      *  [proxy-revalidate] Like the "s-" version of max-age, a "must-revalidate" override only for CDN.
      *  [no-transform]     Some CDNs will optimize images and other formats, this "opt-out" of it.
      *
      *  # Expires
      *  RFC timestamp for an absolute cache expiration, overridden by Cache-Control header.
      *
      *  # ETag
      *  Hash of anything, weak ETags is not supported at this moment.
      *
      *  # vary
      *  Too much fun inside and we are too serious about caching, ignore this.
      *
      *  # pragma
      *  This guy is too old to recognize.
      *  [no-cache] Only this is known nowadays and is already succeed by Cache-Control: no-cache.
      *
      */
     // note; Use "cache-meta://" scheme for header and cache meta info, for performance.
     // 1. Check if cache exists.
     $cache = (array) Cache::get("cache-meta://{$cacheTarget}");
     // Cache expiration, in seconds.
     // expires = ( s-maxage || max-age || Expires );
     if (@$cache['expires'] && time() > $cache['expires']) {
         Cache::delete("cache-meta://{$cacheTarget}");
         Cache::delete("cache://{$cacheTarget}");
         $cache = null;
     }
     // - If not exists, make normal request to remote server.
     // - If exists, make conditional request to remote server.
     //   - Revalidation, we can skip this request and serve the content if false.
     //     revalidates = ( Cache-Control:proxy-revalidate || Cache-Control:must-revalidate )
     if (!$cache || @$cache['revalidates']) {
         $_request = array('uri' => $cacheTarget);
         if ($cache) {
             // Last-Modified
             if (@$cache['headers']['Last-Modified']) {
                 $_request['headers']['If-Modified-Since'] = $cache['Last-Modified'];
             }
             // Entity-Tag
             if (@$cache['headers']['ETag'] && strpos($cache['headers']['ETag'], 'W\\') !== 0) {
                 $_request['headers']['If-None-Match'] = $cache['ETag'];
             }
         } else {
             $cache = array();
         }
         // Make the request
         $_response = new Response(array('autoOutput' => false));
         (new Request($_request))->send(null, $_response);
         unset($_request);
         // parse headers into cache settings.
         if (in_array($_response->status(), array(200, 304))) {
             $res = preg_split('/\\s*,\\s*/', util::unwrapAssoc($_response->header('Cache-Control')));
             $res = array_reduce($res, function ($res, $value) {
                 // todo; Take care of no-cache with field name.
                 if (strpos($value, '=') > 0) {
                     $value = explode('=', $value);
                     $res[$value[0]] = $value[1];
                 } else {
                     $res[$value] = true;
                 }
                 return $res;
             }, array());
             // private, no-store, no-cache
             if (@$res['private'] || @$res['no-store'] || @$res['no-cache']) {
                 // note; in case the upstream server change this to uncacheable
                 Cache::delete("cache-meta://{$cacheTarget}");
                 Cache::delete("cache://{$cacheTarget}");
                 $_response->clearBody();
             }
             if ($_response->status() == 200 && $_response->body()) {
                 $cache['contents'] = $_response->body();
             }
             // expires = ( s-maxage || max-age || Expires );
             if (@$res['s-maxage']) {
                 $cache['expires'] = time() + $res['s-maxage'];
             } elseif (@$res['max-age']) {
                 $cache['expires'] = time() + $res['max-age'];
             } else {
                 $res = util::unwrapAssoc($_response->header('Expires'));
                 if ($res) {
                     $cache['expires'] = strtotime($res);
                 }
             }
             // revalidates = ( Cache-Control:proxy-revalidate || Cache-Control:must-revalidate )
             if (@$res['proxy-revalidate'] || @$res['must-revalidate']) {
                 $cache['revalidates'] = true;
             }
             unset($res);
         }
         $cache['headers'] = array_map('core\\Utility::unwrapAssoc', $_response->header());
         // PHP does not support chunked, skip this one.
         unset($cache['headers']['Transfer-Encoding']);
         // note; If cache is to be ignored, the $cacheTarget variable will be already unset().
         if (isset($cacheTarget)) {
             if (@$cache['contents']) {
                 Cache::set("cache://{$cacheTarget}", $cache['contents']);
             }
             Cache::set("cache-meta://{$cacheTarget}", array_filter_keys($cache, isNot('contents')));
         }
         unset($_response);
     }
     // note; Send cache headers regardless of the request condition.
     if (@$cache['headers']) {
         $response->clearHeaders();
         foreach ($cache['headers'] as $name => $value) {
             $response->header($name, $value, true);
         }
         unset($name, $value);
     }
     // note; Handles conditional request
     $ch = array_map('core\\Utility::unwrapAssoc', (array) @$cache['headers']);
     $mtime = @$ch['Last-Modified'] ? strtotime($ch['Last-Modified']) : false;
     // Request headr: If-Modified-Since
     if (@$ch['Last-Modified'] && $mtime) {
         if (strtotime($request->header('If-Modified-Since')) >= $mtime) {
             return $response->status(304);
         }
     }
     // Request header: If-Range
     if ($request->header('If-Range')) {
         // Entity tag
         if (strpos(substr($request->header('If-Range'), 0, 2), '"') !== false && @$ch['ETag']) {
             if ($this->compareETags(@$ch['ETag'], $request->header('If-Range'))) {
                 return $this->response()->status(304);
             }
         } elseif (strtotime($request->header('If-Range')) === $mtime) {
             return $this->response()->status(304);
         }
     }
     unset($mtime);
     // Request header: If-None-Match
     if (!$request->header('If-Modified-Since') && $request->header('If-None-Match')) {
         // Exists but not GET or HEAD
         switch ($request->method()) {
             case 'get':
             case 'head':
                 break;
             default:
                 return $this->response()->status(412);
         }
         /*! Note by Vicary @ 24 Jan, 2013
          *  If-None-Match means 304 when target resources exists.
          */
         if ($request->header('If-None-Match') === '*' && @$ch['ETag']) {
             return $this->response()->status(304);
         }
         if ($this->compareETags(@$ch['ETag'], preg_split('/\\s*,\\s*/', $request->header('If-None-Match')))) {
             return $this->response()->status(304);
         }
     }
     // Request header: If-Match
     if (!$request->header('If-Modified-Since') && $request->header('If-Match')) {
         // Exists but not GET or HEAD
         switch ($request->method()) {
             case 'get':
             case 'head':
                 break;
             default:
                 return $this->response()->status(412);
         }
         if ($request->header('If-Match') === '*' && !@$ch['ETag']) {
             return $this->response()->status(412);
         }
         preg_match_all('/(?:^\\*$|(:?"([^\\*"]+)")(?:\\s*,\\s*(:?"([^\\*"]+)")))$/', $request->header('If-Match'), $eTags);
         // 412 Precondition Failed when nothing matches.
         if (@$eTags[1] && !in_array($eTag, (array) $eTags[1])) {
             return $this->response()->status(412);
         }
     }
     if ($cacheTarget && empty($cache['contents'])) {
         $cache['contents'] = Cache::get("cache://{$cacheTarget}");
     }
     // Output the cahce content
     $response->send($cache['contents'], 200);
 }