日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

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

發布時間:2023/12/19 php 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php redis编程,php + redis 实现关注功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文:https://www.cnblogs.com/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 2020/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 2020/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 2020/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 2020/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 2020/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 2020/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 2020/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 2020/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);

}

}

總結

以上是生活随笔為你收集整理的php redis编程,php + redis 实现关注功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。