ThinkPHP5之读取hash类型的redis数据并修改
方式一 使用第三方类
RedisHash类下载地址:https://www.kancloud.cn/mikkle/thinkphp5_study/384980
下载放置在vendor目录下,命名空间去掉
$user_id = input('user_id');
$circle_of_friends_id = input('circle_of_friends_id');
vendor('lib.RedisHash');
$hash = new \RedisHash();
$redis = $hash->setTable("club_data")->setHashKey($circle_of_friends_id)->get();//读取所有字段
//$redis = $hash->setTable("club_data")->setHashKey($circle_of_friends_id)->get('total');读取某个或几个字段
$members = $redis['members'];
$members = json_decode($members,true);
foreach ($members as $key => $value) {
if ($value == $user_id) {
unset($members[$key]);
}
}
$members_new = array_values($members);
$members = json_encode($members_new);
$redis['members'] = $members;
$hash->setTable("club_data")->setHashKey($circle_of_friends_id)->set($redis);//修改
方式二 框架自带
//读取配置,初始化类
$this->config = Config('cache');
$this->redis = new \Redis();
$this->redis->connect($this->config['redis']['host'], 6379);
$this->redis->auth($this->config['redis']['password']);
$this->redis->select($this->config['redis']['select']);
$circleOfFriendsRoomCards = $this->redis->hget("club_data:$circle_of_friends_id",'circleOfFriendsRoomCards');//获取
$this->redis->hset("club_data:$circle_of_friends_id",'circleOfFriendsRoomCards',$circleOfFriendsRoomCards);//修改