一:问题:如下图 把所有的医院都放在地图上(每个医院有详细地址,根据详细地址查找出经纬度,遍历在地图上),这个时候就有很多个医院,每次请求无非就慢了
二:解决思路
把所有医院的经纬度放在缓存中,后台对医院增删改的时候都进行经纬度处理
主要用到的:
获取缓存: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; //修改失败
}
}