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; } }
分类:
缓存
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通