200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > php redis编程 php + redis 实现关注功能

php redis编程 php + redis 实现关注功能

时间:2023-07-14 17:54:45

相关推荐

php redis编程 php + redis 实现关注功能

原文:/laowenBlog/p/14192070.html

产品价值

应用场景

在做PC或者APP端时,掺杂点社交概念就有关注和粉丝功能;

数据量小的话数据库还能支持,如果数据量很庞大还是用缓存比较好。

具体实现

1 控制层实现

/**

* 关注/取消关注

* @param Request $request

* @return mixed

*/

public function follow(Request $request)

{

$type = $request->input('type', 'follow'); // 1-关注-follow 2-取消关注-remove

$userId = $request->input('user_id', 0); // 我的用户ID

$otherId = $request->input('other_id', 0); // 我关注的用户ID

if ($userId == $otherId) {

return $this->response->apiResponse();

}

$this->testFollowService->follow($type, $userId, $otherId);

return $this->response->apiResponse();

}

/**

* 我的关注/粉丝

* @param Request $request

* @return mixed

*/

public function myFollowAndFans(Request $request)

{

$type = $request->input('type', 'follow'); // 1-关注-follow 2-粉丝-fans

$userId = $request->input('user_id', 0); // 我的用户ID

$page = $request->input('page', 1); // 页码

$limit = $request->input('limit', 10); // 每页显示条数

$res = $this->testFollowService->myFollowAndFans($userId, $type, $page, $limit);

return $this->response->apiResponse($res);

}

2 服务层实现

/**

* 关注/取消关注

* @param string $type

* @param int $userId

* @param int $otherId

* @return mixed

*/

public function follow($type = 'follow', int $userId, int $otherId)

{

// 关注

if ($type === 'follow') {

$this->testFollowRedis->zAddFollow($userId, $otherId);

$this->testFollowRedis->zAddFans($otherId, $userId);

}

// 取消关注

if ($type === 'remove') {

$this->testFollowRedis->zRemFollow($userId, $otherId);

$this->testFollowRedis->zRemFans($otherId, $userId);

}

}

/**

* 我的关注/粉丝

* @param int $userId 当前登录用户的ID

* @param string $type 要获取的数据

* @param int $page 页码

* @param int $limit 限制条数

* @return array

*/

public function myFollowAndFans(int $userId, $type = 'follow', $page = 1, $limit = 10)

{

$start = $limit * ($page - 1);

$end = $start + $limit - 1;

$res = [];

if ($type === 'follow') {

$res = $this->testFollowRedis->zRangeFollow($userId, $start, $end);

}

if ($type === 'fans') {

$res = $this->testFollowRedis->zRangeFans($userId, $start, $end);

}

return $res;

}

仓储层实现

namespace App\Repository\Redis;

class TestFollowRedis extends AbstractRedis

{

/**

* 关注key

* @var string

*/

private $followKey = '%u:follow';

/**

* 粉丝key

* @var string

*/

private $fansKey = '%u:fans';

/**

* 前缀

*/

public function initPrefix()

{

return 'follow:';

}

/**

* 增加关注

* @param $userId

* @param $otherId

* @author ptg /12/9 5:55 下午

*/

public function zAddFollow($userId, $otherId)

{

$this->redis->zAdd(sprintf($this->prefix . $this->followKey, $userId), time(), $otherId);

}

/**

* 取消关注

* @param $userId

* @param $otherId

* @author ptg /12/9 5:55 下午

*/

public function zRemFollow($userId, $otherId)

{

$this->redis->zRem(sprintf($this->prefix . $this->followKey, $userId), $otherId);

}

/**

* 我的关注 | 正序

* @param int $userId

* @param int $start

* @param int $end

* @return array

* @author ptg /12/9 7:57 下午

*/

public function zRangeFollow(int $userId, int $start = 0, int $end = 9)

{

return $this->redis->zRange(sprintf($this->prefix . $this->followKey, $userId), $start, $end);

}

/**

* 我的关注 | 倒序

* @param int $userId

* @param int $start

* @param int $end

* @return array

* @author ptg /12/9 7:57 下午

*/

public function zRevRangeFollow(int $userId, int $start = 0, int $end = 9)

{

return $this->redis->zRevRange(sprintf($this->prefix . $this->followKey, $userId), $start, $end);

}

/**

* 增加粉丝

* @param $userId

* @param $otherId

* @author ptg /12/9 6:27 下午

*/

public function zAddFans($userId, $otherId)

{

$this->redis->zAdd(sprintf($this->prefix . $this->fansKey, $userId), time(), $otherId);

}

/**

* 移除粉丝

* @param $userId

* @param $otherId

* @author ptg /12/9 6:27 下午

*/

public function zRemFans($userId, $otherId)

{

$this->redis->zRem(sprintf($this->prefix . $this->fansKey, $userId), $otherId);

}

/**

* 我的粉丝 | 正序

* @param int $userId

* @param int $start

* @param int $end

* @return array

* @author ptg /12/9 7:56 下午

*/

public function zRangeFans(int $userId, int $start = 0, int $end = 9)

{

return $this->redis->zRange(sprintf($this->prefix . $this->fansKey, $userId), $start, $end);

}

/**

* 我的粉丝 | 倒序

* @param int $userId

* @param int $start

* @param int $end

* @return array

* @author ptg /12/9 7:56 下午

*/

public function zRevRangeFans(int $userId, int $start = 0, int $end = 9)

{

return $this->redis->zRevRange(sprintf($this->prefix . $this->fansKey, $userId), $start, $end);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。