Commit 1fa3d745 authored by hujun's avatar hujun

缓存

parent 6ed484d5
<?php
/**
* Created by PhpStorm.
* User: 43897
* Date: 2019/4/3
* Time: 10:11
*/
namespace app\api_broker\service;
use app\extra\RedisExt;
use app\model\AAgents;
use app\model\ADistrict;
use app\model\AStore;
use app\model\AuthGroup;
use app\model\AuthRule;
use app\model\Users;
class RedisCacheService
{
private $redis_ext;
private $user_key = 'cache_user_';
private $agent_key = 'cache_agent_';
private $group_key = 'cache_group_';
private $rule_key = 'cache_rule_';
private $time;
public function __construct()
{
$this->redis_ext = RedisExt::getRedis();
$this->time = 604800; //7天
}
/**
* @param $key
* @param $value
* @param int $time
* @return mixed
*/
public function setRedisCache($key, $value, $time = 0)
{
$value = json_encode($value);
$key = $this->key_string.$key;
if (!$time) {
$time = $this->time;
}
return $this->redis_ext->set($key, $value, $time);
}
/**
* @param $key
* @param $value
* @param int $time
* @return mixed
*/
public function setRedisHashCache($key, $value, $time = 0)
{
$value = json_encode($value);
$key = $this->key_string.$key;
if (!$time) {
$time = $this->time;
}
return $this->redis_ext->set($key, $value, $time);
}
/**
* 缓存用户信息
*
* @param $user_id
* @return array|false|\PDOStatement|string|\think\Model
*/
public function userCache($user_id)
{
$m_user = new Users();
$field = 'id,user_nick,user_name,user_phone,user_pic,other_pic,sex,protect_time,site_ids,agent_id,referrer_id,referrer_source';
$field .= ',is_open,industry_type,price_demand,area_demand,status,source,user_status,registration_time,vip,create_time,first_login_time';
$field .= ',source_intro';
$user_data = $m_user->findByOne($field, ['id'=>$user_id]);
if (!empty($user_data)) {
$this->setRedisCache($this->user_key.$user_id, $user_data);
}
return $user_data;
}
/**
* 缓存经纪人信息
*
* @param $agent_id
* @return array|false|\PDOStatement|string|\think\Model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function agentCache($agent_id)
{
$m_agent = new AAgents();
$field = 'id,store_id,auth_group_id,district_id,level,name,phone,img,status,admin_off,site_id';
$data = $m_agent->getAgentInfo($field, $agent_id);
if ($data['district_id']) {
$m_district = new ADistrict();
$data['district_name'] = $m_district->getDistrictKeyById('district_name', ['id'=>$data['district_id']]);
} else {
$data['district_name'] = '';
}
if ($data['store_id']) {
$m_district = new AStore();
$data['store_name'] = $m_district->getStoreKeyById('store_name', ['id'=>$data['store_id']]);
} else {
$data['store_name'] = '';
}
if (!empty($data)) {
$this->setRedisCache($this->agent_key . $agent_id, $data);
}
return $data;
}
public function groupCache($group_id)
{
$m_auth_group = new AuthGroup();
$field = 'id,title,rules';
$data = $m_auth_group->getList2('id asc', $field, ['status'=>0, 'id'=>$group_id]);
if (!empty($data)) {
$this->setRedisCache($this->group_key . $group_id, $data[0]);
}
return $data;
}
public function authCache($rule_id)
{
$m_auth_rule = new AuthRule();
$field = 'id,name,title,pid,is_menu';
$data = $m_auth_rule->getRule($field, ['id'=>$rule_id,'status'=>0]);
if (!empty($data)) {
$this->setRedisCache($this->rule_key . $rule_id, $data[0]);
}
return $data;
}
/**
* @param $type
* @param $id
* @return array|bool|false|mixed|\PDOStatement|string|\think\Model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getRedisCache($type, $id) {
if (empty($id)) {
return false;
}
switch ($type) {
case 1:
$result = $this->redis_ext->get($this->user_key . $id);
if (empty($result)) {
$result = $this->userCache($id);
} else {
$result = json_decode($result, true);
}
break;
case 2 :
$result = $this->redis_ext->get($this->agent_key . $id);
if (empty($result)) {
$result = $this->agentCache($id);
} else {
$result = json_decode($result, true);
}
break;
case 3 :
$result = $this->redis_ext->get($this->group_key . $id);
if (empty($result)) {
$result = $this->groupCache($id);
} else {
$result = json_decode($result, true);
}
break;
case 4 :
$result = $this->redis_ext->get($this->rule_key. $id);
if (empty($result)) {
$result = $this->authCache($id);
} else {
$result = json_decode($result, true);
}
break;
default :
$result = false;
}
return $result;
}
}
\ No newline at end of file
...@@ -733,7 +733,6 @@ die('11111'); ...@@ -733,7 +733,6 @@ die('11111');
$update_data[$k]['phone'] = $v['phone']; $update_data[$k]['phone'] = $v['phone'];
$edit_phone[] = substr_replace($phone, '****', 3, 4) . '修改为;' . substr_replace($v['phone'], '****', 3, 4); $edit_phone[] = substr_replace($phone, '****', 3, 4) . '修改为;' . substr_replace($v['phone'], '****', 3, 4);
$new_phone[] = $phone; $new_phone[] = $phone;
$new_phone[] = $old_landlord[$v['id']];
} }
} else { } else {
$insert_data[$key]['phone'] = $v['phone']; $insert_data[$key]['phone'] = $v['phone'];
......
...@@ -197,8 +197,13 @@ class ADistrict extends BaseModel ...@@ -197,8 +197,13 @@ class ADistrict extends BaseModel
/** /**
* 获取部门 * 获取部门
*
* @param $params * @param $params
* @param $field
* @return false|\PDOStatement|string|\think\Collection * @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/ */
public function getDistrict($params,$field) public function getDistrict($params,$field)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment