苹果cms缓存导航菜单减少数据库查询

云游道人 2025-05-19 485 阅读 0评论

苹果cms缓存导航菜单减少数据库查询

修改:application/common/model/Type.php

找到:

$tmp = Db::name('Type')->where($where)->order($order)->limit($limit_str)->select();

替换为以下代码:

$getType = Cache::get('TypeList');
if(empty($getType)){
   //echo '加入缓存';
   $tmp = Db::name('Type')->where([])->limit(0,999)->select();
   $getType = Cache::set('TypeList',$tmp,3600*3600);
}
$result = array();
foreach ($getType as $item) {
   $matchAllConditions = true;

   foreach ($where as $field => $condition) {
       // 提取条件和值
       $operator = $condition[0];
       $requiredValue = $condition[1];

       // 检查字段是否存在
       if (!isset($item[$field])) {
           $matchAllConditions = false;
           break;
       }

       $itemValue = $item[$field];

       // 根据操作符验证条件
       switch ($operator) {
           case 'eq':
               if ($itemValue != $requiredValue) {
                   $matchAllConditions = false;
               }
               break;
           case 'in':
               // 将字符串条件转为数组,并确保类型一致(如数字转为整型)
               $allowedValues = array_map('intval', explode(',', $requiredValue));
               if (!in_array((int)$itemValue, $allowedValues)) {
                   $matchAllConditions = false;
               }
               break;
           // 可扩展其他操作符(如neq/gt/lt等)
           default:
               $matchAllConditions = false;
               break;
       }

       if (!$matchAllConditions) break;
   }

   if ($matchAllConditions) {
       $result[] = $item;
   }
}

找到:

foreach($tmp as $k=>$v){

替换为:

foreach($result as $k=>$v){

在这个文件继续添加一个重新生成导航菜单缓存的函数:

public function cacheType(){
   $tmp = Db::name('Type')->where([])->limit(0,999)->select();
   Cache::set('TypeList',$tmp,3600*3600);
}

修改:application/admin/controller/Type.php

在(共1处):

return $this->success('ok',null,$extend);

前面添加:(当前菜单发生变动及时更新缓存)

model('Type')->cacheType();

在(共4处):

return $this->success($res['msg']);

前面添加:

model('Type')->cacheType();


日常开发Vlog

发表评论

快捷回复: 表情:
aoman baiyan bishi bizui cahan ciya dabing daku deyi doge fadai fanu fendou ganga guzhang haixiu hanxiao zuohengheng zhuakuang zhouma zhemo zhayanjian zaijian yun youhengheng yiwen yinxian xu xieyanxiao xiaoku xiaojiujie xia wunai wozuimei weixiao weiqu tuosai tu touxiao tiaopi shui se saorao qiudale qinqin qiaoda piezui penxue nanguo liulei liuhan lenghan leiben kun kuaikule ku koubi kelian keai jingya jingxi jingkong jie huaixiao haqian aini OK qiang quantou shengli woshou gouyin baoquan aixin bangbangtang xiaoyanger xigua hexie pijiu lanqiu juhua hecai haobang caidao baojin chi dan kulou shuai shouqiang yangtuo youling
提交
评论列表 (有 0 条评论, 485人围观)

最近发表

热门文章

最新留言

热门推荐

标签列表