syscacheList = array( array('name'=>'模型','function'=>'_contentModel'),//内容模型 array('name' =>'栏目', 'function' => '_category'),//栏目 array('name' =>'导航', 'function' => '_nav'),//导航 array('name' =>'推荐位', 'function' => '_position'),//推荐位 array('name' =>'联动菜单', 'function' => '_linkmenu'),//联动菜单 array('name' =>'类别', 'function' => '_type'),//内容类别 array('name' =>'用户组', 'function' => '_usergroup'),//用户组 array('name' =>'来源', 'function' => '_copyfrom'),//来源 array('name' =>'站点配置', 'function' => '_siteconfig'),//站点 ); } //更新系统缓存 public function actionSyscache() { if (isset($_POST['docache']) || isset($_GET['docache'])) { $page = isset($_GET['page']) ? intval($_GET['page']) : 0; $part = $this->syscacheList[$page]; if($part) { $function = $part['function']; $this->$function(); $result = array('error'=>0,'msg'=>'更新'.$part['name'].'缓存成功..........','data'=>['num'=>($page+1)*12.5]); } else { $result = array('error'=>0,'msg'=>'更新'.$part['name'].'更新全站缓存成功','data'=>['num'=>100]); } echo_json($result); } return $this->render('syscache'); } private function _contentModel() { define('FIELDS_PATH', BASE_PATH.'common'.DIRECTORY_SEPARATOR.'widget'.DIRECTORY_SEPARATOR.'fields'.DIRECTORY_SEPARATOR); $fields = FormElements::getFieldsInfo('types'); //更新内容模型类:表单生成、入库、更新、输出 $class_types = array('form','input','update','output'); foreach($class_types as $class_type) { $cache_data = file_get_contents(FIELDS_PATH.'content_'.$class_type.'.class.php'); $cache_data = str_replace('}?>','',$cache_data); foreach($fields as $field=>$field_value) { if(file_exists(FIELDS_PATH.$field.DIRECTORY_SEPARATOR.$class_type.'.inc.php')) { $cache_data .= file_get_contents(FIELDS_PATH.$field.DIRECTORY_SEPARATOR.$class_type.'.inc.php'); } } $cache_data .= "\r\n } \r\n?>"; file_put_contents(CACHE_PATH.'content_'.$class_type.'.class.php',$cache_data); } //更新模型数据缓存 $array = array(); $sql = "SELECT * FROM {{%content_model}} where disabled=0 order by list_order asc"; $datas= Yii::$app->db->createCommand($sql)->queryAll(); foreach ($datas as $r) { $array[$r['model_id']] = $r; $this->_contentmodel_field($r['model_id']); } Yii::$app->cache->set(CacheId::modelListCacheId(), json_encode($array),0); return true; } private function _category() { $categorys = array(); $models = json_decode(Yii::$app->cache->get(CacheId::modelListCacheId()),true); if(is_array($models)) { foreach ($models as $model_id=>$model) { $sql = "SELECT * FROM {{%category}} where content_model_id=".$model_id." and disabled=0 order by list_order asc"; $datas= Yii::$app->db->createCommand($sql)->queryAll(); $array = array(); foreach ($datas as $r) { $parentModel = Category::findOne($r['parent_id']); if($parentModel)Category::updateParents($r['cat_id']); Category::updateChilds($r['cat_id']); Category::updateChilds($r['parent_id']); Category::updateTypes($r['cat_id']); if($r['cat_type']==1) $array[$r['cat_id']] = $r; } Yii::$app->cache->set(CacheId::categoryItemsCacheId($model_id), json_encode($array),0); } } $sql = "SELECT * FROM {{%category}} where disabled=0 order by list_order ASC"; $categoryResult = Yii::$app->db->createCommand($sql)->queryAll(); if(is_array($categoryResult)) { foreach($categoryResult as $r) { $parentModel = Category::findOne($r['parent_id']); if($parentModel)Category::updateParents($r['cat_id']); Category::updateChilds($r['cat_id']); Category::updateChilds($r['parent_id']); Category::updateTypes($r['cat_id']); $settings = string2array($r['settings']); if(is_array($settings))foreach($settings as $kk=>$vv) { $r[$kk] = $vv; } $r['is_domain'] = '0'; $categorys[$r['cat_id']] = $r; } } Yii::$app->cache->set(CacheId::categoryCacheId(), json_encode($categorys),0); return true; } private function _position() { $array = array(); $sql = "SELECT * FROM {{%position}} order by list_order asc"; $lists= Yii::$app->db->createCommand($sql)->queryAll(); if($lists)foreach($lists as $_value) { $array[$_value['position_id']] = $_value; } Yii::$app->cache->set(CacheId::positionCacheId(), json_encode($array),0); return true; } private function _nav() { $array = array(); $sql = "SELECT * FROM {{%nav}} where disabled=0 order by list_order asc"; $lists= Yii::$app->db->createCommand($sql)->queryAll(); if($lists)foreach($lists as $_value) { $array[$_value['type']][] = $_value; } Yii::$app->cache->set(CacheId::navCacheId(), json_encode($array),0); return true; } private function _linkmenu() { return true; } private function _type() { $array = array(); $sql = "SELECT * FROM {{%category_type}} where disabled=0 order by list_order asc"; $lists= Yii::$app->db->createCommand($sql)->queryAll(); if(is_array($lists))foreach($lists as $_value) { $array[$_value['type_id']] = $_value; } Yii::$app->cache->set(CacheId::typeCacheId(), json_encode($array),0); return true; } private function _usergroup() { //更新模型数据缓存 $array = array(); $sql = "SELECT * FROM {{%user_group}} where disabled=0 order by list_order asc"; $datas= Yii::$app->db->createCommand($sql)->queryAll(); foreach ($datas as $r) { $array[$r['group_id']] = $r; } Yii::$app->cache->set(CacheId::groupCacheId(), json_encode($array),0); return true; } private function _copyfrom() { $array = array(); $sql = "SELECT * FROM {{%copy_from}} order by list_order asc"; $datas= Yii::$app->db->createCommand($sql)->queryAll(); if(is_array($datas))foreach ($datas as $r) { $array[$r['id']] = $r; } Yii::$app->cache->set(CacheId::copyFromCacheId(), json_encode($array),0); return true; } private function _siteconfig() { return true; } //模型字段缓存 private function _contentmodel_field($model_id) { $array = array(); $sql = "SELECT * FROM {{%content_model_field}} where disabled=0 and model_id=".$model_id." order by list_order asc"; $fields= Yii::$app->db->createCommand($sql)->queryAll(); foreach($fields as $_value) { $setting = string2array($_value['setting']); $_value = array_merge($_value,$setting); $array[$_value['field']] = $_value; } Yii::$app->cache->set(CacheId::modelFieldCacheId($model_id), json_encode($array),0); return true; } //更新栏目子级信息 private function _updateChilds($id) { if($id==0)return; $parentModel = Category::findOne($id); if($parentModel) { $childs = array(); $childs[]=$id; $categoryList = Category::find()->where('parent_id='.$id)->all(); if(is_array($categoryList)) { foreach($categoryList as $category) { $childs[] = $category['cat_id']; } } if(!empty($childs)&&count($childs)>1) { //asort($childs); $parentModel->arr_child_ids =join(',',$childs); $parentModel->has_child = 1; $parentModel->save(); } else { $parentModel->arr_child_ids =''; $parentModel->has_child = 0; $parentModel->save(); } } } //更新栏目父级信息 private function _updateParents($id,$parents = array()) { $model = Category::findOne($id); if($model) { if(empty($parents)){ $parents[] = $id; if($model->parent_id>0)$parents[]=$model->parent_id; } if(count($parents)==1) { $model->arr_parent_ids = ''; $model->save(); return; } else { $parentModel = Category::findOne($parents[count($parents)-1]); if($parentModel->parent_id==0) { $arr_parent_id =','.join(',',$parents).','; $model->arr_parent_ids = trim($arr_parent_id,','); $model->save(); return; } else { $parents[] = $parentModel->parent_id; return $this->_updateParents($id,$parents); } } } } }