Tp5缓存Cache

一、应用缓存获取城市信息

/**
     * 获取城市下属地址分类信息
     * @param string $cityid
     * @return array|mixed
     */
    public function superCityPlace($cityid=""){
        $this->cache = false;
        if (empty($cityid)||!is_numeric($cityid)){
            return self::showReturnCodeWithOutData(1003);
        }
        if ($this->cache==true && Cache::has("city_place_$cityid")){
            return Cache::get("city_place_$cityid");
        }else{
            $area=Db::table("my_area")->where(["cityid"=>$cityid])->field("areaid,areaname")->order('displayorder')->select();
            $place = [] ;
            foreach($area as $item=>$value ){
                $street_list = $this->getStreetByAreaid($value["areaid"]);
                if ($street_list){
                    $place[]=[
                        "areaid"=>$value["areaid"],
                        "areaname"=>$value["areaname"],
                        "street_list"=>$this->getStreetByAreaid($value["areaid"]),
                    ];
                }
            }
            Cache::set("city_place_$cityid",$place);
            return self::showReturnCode(1001,$place );
        }
    }

    /**
     * @param $areaid
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    private function getStreetByAreaid($areaid){

        if (empty($areaid)||!is_numeric($areaid)){
            return [];
        }else{
            if ($this->cache==true && Cache::has("area_place_$areaid")){
                return Cache::get("area_place_$areaid");
            }else {
                $place=Db::table("my_street")->where(["areaid" => $areaid])->field("streetid,streetname")->order('displayorder')->select();
                Cache::set("area_place_$areaid", $place);
                return $place;
            }
        }
    }

二 、应用缓存获取分类详情

 /**
     * 缓存分类详情
     * @param $catid
     * @return array|bool|mixed
     */
    public function getCategoryInfoByCatid($catid){
        if(is_null($catid) || !is_numeric($catid)){
            return false;
        }
        if ($this->cache==true && Cache::has("category_info_$catid")){
            return Cache::get("category_info_$catid");
        }else{
            $category =Category::get($catid);
            if (empty($category)) return false;
            $info_table = $this->getInfoTable($category["modid"]);
            $options_ids = $this->getCategoryOptionsIds($category["modid"]);
            $search_field = $this->getCategorySearchFieldList($options_ids);
            $category_info=[
                "catid" =>$catid,                           //分类ID
                "sub_catids"=>$this->getSubCatids($catid),  //获取所有子类ID
                "catname"=>$category["catname"],
                "modid"=>$category["modid"],
                "dir_typename"=>$category["dir_typename"],
                "info_table"=>$info_table,                  //获取子表名
                "search_field"=>$search_field,              //获取分类搜索字段
                "publish_field"=>array_merge(Config::get("public_field_list"),$search_field),  //获取发布信息字段
                "options_ids"=>$options_ids,                //获取参数字段
                "options_list"=>$this->getCategoryOptionsListByIds($options_ids), //获取参数值
            ];
            Cache::set("category_info_$catid",$category_info);
            return $category_info;
        }
    }

猜你喜欢

转载自www.cnblogs.com/swmin/p/9951776.html
tp5
今日推荐