diff --git a/config/cache.php b/config/cache.php new file mode 100644 index 0000000000..3906d822c1 --- /dev/null +++ b/config/cache.php @@ -0,0 +1,16 @@ +cache = new stdclass(); +$config->cache->enable = false; // 是否开启缓存。Enable cache or not. +$config->cache->lifetime = 5 * 60; // 缓存生存时间。The lifetime of cache. +$config->cache->driver = 'File'; // 缓存驱动。 The driver of cache. can be File|Yac|Apcu. + +$config->cache->enableFullPage = false; // 是否开启整页缓存。Enable full page cache or not. +$config->cache->fullPageLifetime = 5 * 60; +$config->cache->fullPageDriver = 'File'; + +$config->cache->fullPages = array(); +$config->cache->fullPages[] = 'program|browse'; +$config->cache->fullPages[] = 'product|all'; +$config->cache->fullPages[] = 'project|browse'; diff --git a/config/config.php b/config/config.php index 07552ee709..43b36f6066 100644 --- a/config/config.php +++ b/config/config.php @@ -71,12 +71,6 @@ $config->slaveDB->driver = 'mysql'; $config->slaveDB->encoding = 'UTF8'; $config->slaveDB->strictMode = false; -/* 缓存设置。Cache settings. */ -$config->cache = new stdclass(); -$config->cache->enable = false; // 是否开启缓存。Enable cache or not. -$config->cache->lifetime = 5 * 60; // 缓存生存时间。The lifetime of cache. -$config->cache->driver = 'File'; // 缓存驱动。 The driver of cache. can be File|Yac|Apcu. - /* 可用域名后缀列表。Domain postfix lists. */ $config->domainPostfix = "|com|com.cn|com.hk|com.tw|com.vc|edu.cn|es|"; $config->domainPostfix .= "|eu|fm|gov.cn|gs|hk|im|in|info|jp|kr|la|me|"; @@ -177,6 +171,10 @@ if(file_exists($filterConfig)) include $filterConfig; $dbConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'db.php'; if(file_exists($dbConfig)) include $dbConfig; +/* 引用缓存的配置。 Include the cache config file. */ +$cacheConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cache.php'; +if(file_exists($cacheConfig)) include $cacheConfig; + /* 引用自定义的配置。 Include the custom config file. */ $myConfig = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my.php'; if(file_exists($myConfig)) include $myConfig; diff --git a/framework/base/router.class.php b/framework/base/router.class.php index 2204c50954..822d5669f3 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -2250,11 +2250,48 @@ class baseRouter return $module; } catch (EndResponseException $endResponseException) { echo $endResponseException->getContent(); + return false; } return isset($module) ? $module : false; } + /** + * 输出内容。 + * Output the content. + * + * @return string + */ + public function outputPage() + { + $cacheEnable = $this->config->cache->enableFullPage; + /* If caching is not turned on, pages that do not need to be cached, or when searching, they are not cached. */ + if(!$cacheEnable || !in_array("{$this->moduleName}|{$this->methodName}", $this->config->cache->fullPages) || stripos($this->server->request_uri, 'search') !== false) + { + $this->loadModule(); + return helper::removeUTF8Bom(ob_get_clean()); + } + + $this->loadClass('cache', $static = true); + $cacheKey = md5($this->server->request_uri); + $namespace = isset($this->session->user->account) ? $this->session->user->account : 'guest'; + $cache = cache::create($this->config->cache->fullPageDriver, $namespace, $this->config->cache->fullPageLifetime); + + if($cache->has($cacheKey)) + { + $content = $cache->get($cacheKey); + } + else + { + ob_start(); + $result = $this->loadModule(); + $content = helper::removeUTF8Bom(ob_get_clean()); + /* If the module is loaded successfully, cache the content. */ + if($result !== false) $cache->set($cacheKey, $content); + } + return $content; + } + /** * 加载指定模块下的某种对象。 * Load the target object of one module. diff --git a/www/index.php b/www/index.php index cabae829c6..7be7a1f1fe 100644 --- a/www/index.php +++ b/www/index.php @@ -72,7 +72,6 @@ $app->parseRequest(); if(!$app->setParams()) return; $common->checkPriv(); $common->checkIframe(); -$app->loadModule(); /* Flush the buffer. */ -echo helper::removeUTF8Bom(ob_get_clean()); +echo $app->outputPage();