400-915-1135
详细

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

发表日期:2022-06-08 15:52:01   作者来源:超级管理员   浏览:0

https://www.htmler.top/pbootcms/634.html

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

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

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

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

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

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

image.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>

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


注意:这种方法有一个缺点就是如果你正在网站操作网站后台, 访问前端页面触发清理后会强制退出后台


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

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


开发版已集成该功能并不会造成清理后强制退出当前后台,了解开发版

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


末尾附上完整带日志清理与缓存清理的 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);
        }
    }
    
}



本文章多为网络内容整理而来,如有侵犯您的权益,请联系我们免费删除