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 实现关注功能相关推荐
- 项目1在线交流平台-4. 使用radis高性能储存方案-5.redis常用使用场景-开发关注功能-redis事务
文章目录 功能需求 1. dao层设计redis对应的key 设计储存关注对象信息的健值对 key value 设计储存粉丝信息的健值对 key value 2. Service层处理关注和取关的业务 ...
- Redis实战之好友关注功能
目录 1.关注和取消关注 2.好友关注 - 共同关注 3.好友关注 - Feed 流实现方案 4.好友关注 - 推送到粉丝收件箱 1)传统了分页在 feed 流是不适用的,因为我们的数据会随时发生变化 ...
- Redis实战案例及问题分析之好友关注功能(关注、共同好友、消息推送)
关注和取关 需求:基于该表数据结构,实现两个接口 关注和取关接口 @Overridepublic Result follow(Long followUserId, Boolean isFollow) ...
- php粉丝关注功能,Redis实现用户关注功能
最近项目要涉及到粉丝关注问题,权衡再三还是使用Redis实现比较方便,使用Redis的有序集合可以做到根据关注的时间有序的取出列表,假设我的ID是me,别人的ID是other. 添加关注 添加关注分为 ...
- Redis实战之微博关注功能
1.需求分析: 每个用户都有两个表:用户的关注.用户的粉丝 关注关系有四种:关注.粉丝.互粉.无关系 关注功能可划分为: 1)查看自己的关注/粉丝列表: 关注列表与自己只存在两种关系:关注.互粉 粉丝 ...
- 操作Redis客户端工具详解之功能介绍及配置
问题背景 日常开发过程中,对于缓存,我们并不陌生.常用的缓存有个Redis.memcache.memcached等.那么操作缓存的工具又有很多,我们该怎么选择呢? 今天我们聊一下Redis的操作客户端 ...
- java签到程序设计_java redis 实现简单的用户签到功能
业务需求是用户每天只能签到一次,而且签到后用户增加积分,所以把用户每次签到时放到redis 缓存里面,然后每天凌晨时再清除缓存,大概简单思想是这样的 直接看代码吧如下 @Transactional @ ...
- redis使用watch完成秒杀抢购功能
redis使用watch完成秒杀抢购功能: 使用redis中两个key完成秒杀抢购功能,mywatchkey用于存储抢购数量和mywatchlist用户存储抢购列表. 它的优点如下: 1. 首先选用内 ...
- php生成 sku_高并发下,php与redis实现的抢购、秒杀功能
抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易想到 ...
最新文章
- 基于开源TiRG的文本检测与提取实现
- linux 定时任务
- 阿里云上测试服务器的搭建
- 标签的宽度_27 表格标签
- Spring Boot(四)Spring Boot @ConfigurationProperties实例
- JAVA格式化当前日期或者取年月日
- 2020 China Collegiate Programming Contest Qinhuangdao Site 补题部分
- js算法初窥03(搜索及去重算法)
- js设置了location.href不跳转问题
- OpenWares | Open Source and Free Matters » 为rm命令增加回收站功能
- 组态软件运行在云服务器是上,如何将本地组态画面发布到云服务器
- Android7.1编译SDK报错解决方法总结
- Java NIO学习与记录(七): Reactor单线程模型的实现
- 汇编语言 王爽 【第四版】 第一章 检测点1.1
- 沃谈小知识|可“防拆”的远程锁机
- Python安全攻防-从入门到入狱
- mysql字段描述_详细的MySQL字段类型描述
- 手机充当电脑 麦克风 扬声器 摄像头
- 网络状态检测的利器 - ss命令
- 非服务器模式下运行getImageData函数出现 the operation is insecure
热门文章
- jasmine.any(Function)
- Web 应用服务器端渲染入门指南
- Service Worker 概念简介
- Angular routerLink指令的href属性生成逻辑
- jasmine.spy对象的and.returnValue方法单步调试
- 我的SAP Hybris学习笔记
- 微信开发系列之十 - 在SAP C4C接收微信发送过来的服务请求回应
- SAP ABAP和Java里的弱引用(WeakReference)和软引用(SoftReference)
- escape in ABAP and JavaScript
- SAP CRM Business Partner 自动决定Determination的执行逻辑