帝国cms高效率随机调用函数,解决慢的问题(毫秒级调用)

帝国cms全站高效率随机调用特点:60万数据随机调用测试,文件读取调用,调用效率高,调用可指定栏目调用,也可随机调用,笔者以起名网www.hmzw.com 为例,介绍具体操作。
1.安装方式
  将代码放到目录的e/class/userfun.php文件中的最后。
2.php代码
function ecms_rand($classid, $num) {
    global $empire, $dbtbpre;
    $file_cache = ECMS_PATH . "d/news.json"; // 缓存文件
    $cache_expiration_time = 3600; // 缓存过期时间,单位:秒,这里设置为1小时

    // 检查缓存文件是否存在
    if (file_exists($file_cache)) {
        // 获取缓存文件的内容
        $cache_content = file_get_contents($file_cache);
        $cache_data = json_decode($cache_content, true);

        // 检查缓存是否包含时间戳
        if (isset($cache_data['timestamp'])) {
            $current_time = time();
            // 检查缓存是否过期
            if ($current_time - $cache_data['timestamp'] > $cache_expiration_time) {
                // 缓存已过期,需要重新生成
                $rebuild_cache = true;
            } else {
                // 缓存未过期,直接使用缓存数据
                $news_data = $cache_data['data'];
                $rebuild_cache = false;
            }
        } else {
            // 缓存文件不包含时间戳,重新生成缓存
            $rebuild_cache = true;
        }
    } else {
        // 缓存文件不存在,需要重新生成
        $rebuild_cache = true;
    }

    // 重新生成缓存
    if ($rebuild_cache) {
        $news_data = [];
        // 查询分类
        $hm_class = $empire->query("select classid from {$dbtbpre}enewsclass");
        while ($hm_r = $empire->fetch($hm_class)) {
            // 查询分类下面的所有id
            $hm_news = $empire->query("SELECT id FROM `{$dbtbpre}ecms_news` WHERE `classid` = {$hm_r['classid']}");
            while ($hm_nr = $empire->fetch($hm_news)) {
                $news_data[$hm_r['classid']][] = $hm_nr['id'];
            }
        }

        // 构建包含时间戳和数据的缓存数组
        $cache_data = [
            'timestamp' => time(),
            'data' => $news_data
        ];

        // 将缓存数据写入文件
        file_put_contents($file_cache, json_encode($cache_data));
    }

    $selected_ids = [];
    if ($classid === 0) {
        // 当 $classid 为 0 时,从所有分类中随机选取
        $all_ids = [];
        foreach ($news_data as $ids) {
            $all_ids = array_merge($all_ids, $ids);
        }

        // 如果所有新闻 ID 数量小于需要的数量,就取全部
        if (count($all_ids) <= $num) {
            $selected_ids = $all_ids;
        } else {
            // 随机打乱数组
            shuffle($all_ids);
            // 取前 $num 个元素
            $selected_ids = array_slice($all_ids, 0, $num);
        }
    } else {
        // 当 $classid 不为 0 时,获取指定分类的数据
        if (isset($news_data[$classid])) {
            $ids = $news_data[$classid];
            // 如果指定分类下新闻 ID 数量小于需要的数量,就取全部
            if (count($ids) <= $num) {
                $selected_ids = $ids;
            } else {
                // 随机打乱数组
                shuffle($ids);
                // 取前 $num 个元素
                $selected_ids = array_slice($ids, 0, $num);
            }
        }
    }

    // 将选中的 ID 数组转换为以逗号分隔的字符串,并去除末尾逗号
    $rand_id = implode(',', $selected_ids);
    return rtrim($rand_id, ",");
}
3.调用方式:
// 假设 $num 是你要获取的随机新闻 ID 数量
$num = 5;

// 获取指定分类的数据
$classid = 1;
$random_ids_str = ecms_rand($classid, $num);
echo $random_ids_str;

// 随机输出数据
$classid = 0;
$random_ids_str = ecms_rand($classid, $num);
echo $random_ids_str;
4、说明

    实用性不错自行测试,带缓存过期,更加方便每次更新文章之后。

本文根据:帝国cms全站高效率随机调用函数代码(毫秒级调用)-CSDN博客 的思路重写。感谢。

你可能感兴趣的:(php)