一站式百度SEO排名优化!-找老刘博客 低投入,高转化,精益求精、一丝不苟:旨在提供更好的SEO服务!

首页>>前端开发

PbootCMS简单两步增加自动清理缓存功能(末尾附文件全代码)

首页 2022-06-29 前端开发 1942 ℃Tags:


自PbootCmsV2.0.6开始,PbootCMS支持自定义标签,且升级不被覆盖。

image.png

妈妈再也不用担心我的代码升级被覆盖啦。

于是就想到用这个功能来实现实现自动清理runtime下的文件,防止缓存过多导致空间存储不足。

这个文件位置在 home下 ExtLabelController 控制器。

原理也不多说,反正大家也不想了解,那就直接进入主题

第一步:打开/apps/home/controller/ExtLabelController.php文件后,在下图红圈位置下方添加代码,大概36行

1656470216766597.png

// 自动会话清理脚本
public
function clean_session() {
    check_dir(RUN_PATH.'/archive', true);
    $data = json_decode(trim(substr(file_get_contents(RUN_PATH.'/archive/session_ticket.php'), 15)));
    if ($data) {
        if ($data - >expire_time && $data - >expire_time < time()) {
            ignore_user_abort(true);
            set_time_limit(7200);
            ob_start();
            ob_end_flush();
            flush();
            $rs = path_delete(RUN_PATH.'/session');
            if ($rs) {
                $data - >expire_time = time() + 60 * 60 * 24; // 下一次清理时间
                create_file(RUN_PATH.'/archive/session_ticket.php', "<?php exit();?>".json_encode($data), true);
            }
        }
    } else {
        $data - >expire_time = time() - 60 * 60 * 24; // 初始化清理时间
        create_file(RUN_PATH.'/archive/session_ticket.php', "<?php exit();?>".json_encode($data), true);
    }
}

第二步:在前端公共模板底部添加下面代码,至于为什么不直接在ExtLabelController控制器run函数中调用,别问那么多,就算你问我也不说

<script src='/?p=/ExtLabel/clean_session/' async='async'></script>

保存文件,完成!这样每天第一个访问你网站就会触发自动清理脚本,如果上次清理时间是一天前(时间可自行设置),就会执行自动清理


注意:这种方法有一个缺点就是如果你正在网站操作网站后台, 访问前端页面触发清理后会强制退出后台,但几乎影响不到,一天就触发一次,大概率半夜就触发了。


问:网站没人访问触发不了清理怎么办?

答:我建议你不要关心这个,都没人访问你的网站,你还有个锤子东西需要清理?

问:听人说前台插入script触发会不安全?

答:都是成年人了其中的猫腻自行辨别,或者你需要交点智商税,该触发方式源于官方的蜘蛛记录与文章浏览量。

问:有没有非前台插入script的方法?

答:有但不推荐,非script无非就是在公共控制类里面执行吧,但是官方更新会覆盖。非要杠的话把函数里的代码复制到公共控制类里面去就行。


注:不知道你学废了没有,如果你还没学废,可以请博主吃个快餐有偿代劳

末尾附上完整带日志清理与缓存清理的 ExtLabelController.php 文件代码

< ?php
/**
 * @copyright (C)2020-2099 Hnaoyun Inc.
 * @author XingMeng
 * @email hnxsh@foxmail.com
 * @date 2020年3月8日
 *  个人扩展标签可编写到本类中,升级不会覆盖
 */
namespace apphomecontroller;
use coreasicController;
class ExtLabelController {
    protected $content;
    /* 必备启动函数 */
    public
    function run($content) {
        // 接收数据
        $this - >content = $content;

        // 执行个人自定义标签函数
        // $this->test();
        // 返回数据
        return $this - >content;
    }
    // 测试扩展单个标签
    private
    function test() {
        $this - >content = str_replace('119.126.107.48', get_user_ip(), $this - >content);
    }

    // 自动会话清理脚本
    public
    function clean_session() {
        check_dir(RUN_PATH.'/archive', true);
        $data = json_decode(trim(substr(file_get_contents(RUN_PATH.'/archive/session_ticket.php'), 15)));
        if ($data) {
            if ($data - >expire_time && $data - >expire_time < time()) {
                ignore_user_abort(true);
                set_time_limit(7200);
                ob_start();
                ob_end_flush();
                flush();
                $rs = path_delete(RUN_PATH.'/session');
                if ($rs) {
                    $data - >expire_time = time() + 60 * 60 * 24; // 下一次清理时间
                    create_file(RUN_PATH.'/archive/session_ticket.php', "<?php exit();?>".json_encode($data), true);
                }
            }
        } else {
            $data - >expire_time = time() - 60 * 60 * 24; // 初始化清理时间
            create_file(RUN_PATH.'/archive/session_ticket.php', "<?php exit();?>".json_encode($data), true);
        }
    }

    // 自动日志清理脚本
    public
    function clean_log() {
        $interval = '3'; // 每隔多少天清理一次
        $retain = '7'; // 需要删除多少天前的日志
        check_dir(RUN_PATH.'/archive', true);
        $data = json_decode(trim(substr(file_get_contents(RUN_PATH.'/archive/log_ticket.php'), 15)));
        if ($data) {
            if ($data - >expire_time && $data - >expire_time < time()) {
                $startday = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d') - $retain, date('Y')));
                $rs = coreasicDb: :table('ay_syslog') - >where("create_time<'$startday'") - >delete();
                if ($rs) {
                    $data - >expire_time = time() + 60 * 60 * 24 * $interval; // 下一次清理时间
                    create_file(RUN_PATH.'/archive/log_ticket.php', "<?php exit();?>".json_encode($data), true);
                }
            }
        } else {
            $data - >expire_time = time() - 60 * 60 * 24 * $interval; // 初始化清理时间
            create_file(RUN_PATH.'/archive/log_ticket.php', "<?php exit();?>".json_encode($data), true);
        }
    }

}

出处:https://www.htmlbk.com/pbootcms/634.html

搜索
分类
热门标签
  • 首页
  • 电话
  • QQ
  • 联系老刘手机
    1043025812
    联系老刘微信
    扫描微信二维码