+ Support full page cache.

This commit is contained in:
Lufei
2023-06-05 16:52:54 +08:00
parent 4036b05def
commit cae4cc315a
4 changed files with 58 additions and 8 deletions
+16
View File
@@ -0,0 +1,16 @@
<?php
/* 缓存设置。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.
$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';
+4 -6
View File
@@ -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;
+37
View File
@@ -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.
+1 -2
View File
@@ -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();