使用方法

打开文件 admin/index.php 找到第 115 行的如下代码:

html = cache ? cache.getItem('feed') : '',

将以上代码替换为以下代码:

html = '',

打开文件 var/Widget/Ajax.php 搜索找到 function feed() 将函数 {...} 内的所有代码,替换为以下代码(留意注释,以及按需修改):

 $cacheFilePath = __TYPECHO_ROOT_DIR__ . '/usr/cache/Typecho_feed_cache.json'; // 将官方最新日志数据存储在 usr/cache 目录的 Typecho_feed_cache.json 文件中,自行修改
$cacheExpiration = 180*24*60*60; // 缓存半年,自行修改

if (file_exists($cacheFilePath) && (time() - filemtime($cacheFilePath)) < $cacheExpiration) {
    $data = json_decode(file_get_contents($cacheFilePath), true);
    $this->response->throwJson($data);
} else {
    $this->user->pass('subscriber');
    $client = Client::get();
    if ($client) {
        $client->setHeader('User-Agent', $this->options->generator)
            ->setTimeout(10)
            ->send('https://typecho.org/feed/');

        // 匹配内容体
        $response = $client->getResponseBody();
        preg_match_all(
            "/<item>\s*<title>([^>]*)<\/title>\s*<link>([^>]*)<\/link>\s*<guid>[^>]*<\/guid>\s*<pubDate>([^>]*)<\/pubDate>/is",
            $response,
            $matches
        );

        $data = [];

        if ($matches) {
            foreach ($matches[0] as $key => $val) {
                $data[] = [
                    'title' => $matches[1][$key],
                    'link'  => $matches[2][$key],
                    'date'  => date('n.j', strtotime($matches[3][$key]))
                ];

                if ($key > 8) {
                    break;
                }
            }
        }

        file_put_contents($cacheFilePath, json_encode($data));
        $this->response->throwJson($data);
    }
}

throw new Exception(_t('禁止访问'), 403);

注意:以上代码中,未对存储文件夹做是否可写的判断,所以,请确保文件夹可以创建缓存文件。

End

本文标题: Typecho 文档:加快后台打开速度

本文链接: https://blog.kisenhz.cn/archives/24.html

除非另有说明,本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

声明:转载请注明文章来源。

最后修改:2024 年 12 月 15 日
如果觉得我的文章对你有用,请随意赞赏