tp5.1 file 缓存实例

3 篇文章 0 订阅

一:问题:如下图 把所有的医院都放在地图上(每个医院有详细地址,根据详细地址查找出经纬度,遍历在地图上),这个时候就有很多个医院,每次请求无非就慢了
在这里插入图片描述

二:解决思路

把所有医院的经纬度放在缓存中,后台对医院增删改的时候都进行经纬度处理
主要用到的:
获取缓存:Cache::get('name'); 
设置缓存:Cache::set('name');
获取并删除缓存:Cache::pull('name');
数组搜索:$key = aarray_search('要搜索的数据','被搜索的数组');
删除数组中的某项数据,并自动序列化:array_splice('数组','数组的键',1);

三:解决方法
直接上代码:
要引用cache类,记得要加 facade,并且Cache的首字母要大写

use think\facade\Cache;

    /**
     * 所有医院
     * @return array
     */
    public function allhospital()
    {
        if(Cache::get('allpositials')){
            $allpositials = Cache::get('allpositials');
        }else{
            $allhos = Db::table('kit_hospital')->column('hospital_address');
            $allpositials = [];
            foreach($allhos as $k=>$v){
                $allpositials[$k] = addresstolatlag($v);
            }
            Cache::set('allpositials',$allpositials);
        }
        return $allpositials;
    }

添加数据的时候

    /**
     * 添加
     * @param Request $request
     * @return array|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add_img(Request $request)
    {
        if ($request->isAjax()) {
            //取所有值 先验证
            $arr = json_decode($request->param('data'), true);
            //验证参数
            $validate = new HospitalValidate();
            if (!$validate->scene('add_img')->check($arr)) {
                return ($validate->getError());
            }

            $model = new HospitalModel();
            $info = $model->add_img($arr);
            //添加成功,更新医院经纬度缓存
            if($info == 1){
                if(Cache::get('allpositials')){
                    $allpositial = addresstolatlag($arr['hospital_address']);
                    $allpositials = Cache::pull('allpositials');
                    $allpositials[] = $allpositial;
                    Cache::set('allpositials',$allpositials);
                }
            }
            return $info;
        }
        //全国省份
        if(Cache::get('province')){
            $province = Cache::get('province');
        }else{
            $citymodel = new CityModel();
            $province = $citymodel->find($date=1);
            Cache::set('province',$province);
        }
        $this->assign('province',$province);
        return $this->fetch();
    }

删除数据的时候

    public function img_del($date)
    {
        $del  = $this->where('id', $date)->delete();
        if ($del) {
            if(Cache::get('allpositials')){
                $hospitalinfo = $this->where('id',$date)->find();
                $allpositials = Cache::get('allpositials');
                $oldpositial  = addresstolatlag($hospitalinfo['hospital_address']);
                $key = array_search($oldpositial,$allpositials);
                array_splice($allpositials,$key,1); //删除 $key 自动序列化
            }
            return 1; //删除成功
        } else {
            return 2;//删除失败
        }
    }

修改数据的时候:

    public function updateedit($date)
    {
        //去空格,查找信息是否已经录入
        $date = trim_array_element($date);
        $where['hospital_name']       = $date['hospital_name'];
        $where['hospital_department'] = $date['hospital_department'];
        $where['hospital_province']   = $date['hospital_province'];
        $where['hospital_city']       = $date['hospital_city'];
        $where['hospital_type']       = $date['hospital_type'];
        $where['hospital_address']    = $date['hospital_address'];
        $allinfo = $this->where($where)->find();
        if($allinfo['id'] != $date['id'] && $allinfo){
            return 200;
        }
        //执行更新
        $date['update_time'] = date('Y-m-d H:i:s', time());
        $info = $this->allowField(true)->isUpdate(true)->update($date);
        if ($info) {
            //更新成功,修改地址,更新缓存 => 判断之前缓存数据是否存在,删除,并添加新的
            $hospitalinfo = $this->where('id','=', $date['id'])->find();
            if($date['hospital_address'] != $hospitalinfo['hospital_address']){
                if(Cache::get('allpositials')){
                    $allpositials = Cache::get('allpositials');
                    $oldpositial  = addresstolatlag($hospitalinfo['hospital_address']);
                    $newpositial  = addresstolatlag($date['hospital_address']);
                    $key = array_search($oldpositial,$allpositials);
                    array_splice($allpositials,$key,1); //删除 $key 自动序列化
                    //添加新的位置
                    $allpositials[] = $newpositial;
                }
            }
            return 1; //修改成功
        } else {
            return 11; //修改失败
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值