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;
        }
    }
复制代码

 

posted @   小H先生  阅读(1729)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示