1. 程式人生 > >Tp5快取Cache

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;
        }
    }