产品价值

1: 关注功能

2: 功能分析之“关注”功能

3: 平平无奇的「关注」功能,背后有4点重大价值

应用场景

在做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;
}

仓储层实现

<?php

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 实现关注功能的更多相关文章

  1. Redis的各项功能解决了哪些问题?

    先看一下Redis是一个什么东西.官方简介解释到:Redis是一个基于BSD开源的项目,是一个把结构化的数据放在内存中的一个存储系统,你可以把它作为数据库,缓存和消息中间件来使用.同时支持string ...

  2. 4个点让你彻底明白Redis的各项功能

    前言 先看一下Redis是一个什么东西.官方简介解释到: Redis是一个基于BSD开源的项目,是一个把结构化的数据放在内存中的一个存储系统,你可以把它作为数据库,缓存和消息中间件来使用.同时支持st ...

  3. 【转】Redis的各项功能解决了哪些问题?

    作者:Blackheart 出处:http://linianhui.cnblogs.com 先看一下Redis是一个什么东西.官方简介解释到:Redis是一个基于BSD开源的项目,是一个把结构化的数据 ...

  4. Redis的各项功能解决了哪些问题?(转)

    http://www.cnblogs.com/linianhui/p/what-problem-does-redis-solve.html 先看一下Redis是一个什么东西.官方简介解释到:Redis ...

  5. Redis多机功能介绍

    Redis多机功能目的:以单台Redis服务器过渡到多台Redis服务器 Redis单机在生产环境中存在的问题 1.内存容量不足 Redis使用内存来存书数据库中的数据,但是对于一台机器来说,硬件的内 ...

  6. Redis的事务功能详解

    Redis的事务功能详解 MULTI.EXEC.DISCARD和WATCH命令是Redis事务功能的基础.Redis事务允许在一次单独的步骤中执行一组命令,并且可以保证如下两个重要事项: >Re ...

  7. Redis实现排行榜功能(实战)

    需求前段时间,做了一个世界杯竞猜积分排行榜.对世界杯64场球赛胜负平进行猜测,猜对+1分,错误+0分,一人一场只能猜一次.1.展示前一百名列表.2.展示个人排名(如:张三,您当前的排名106579). ...

  8. Redis实现聊天功能

    在学习了Redis做为消息队列之后研究 了redis聊天的功能. 其实用关系型数据库也可以实现消息功能,自己就曾经用mysql写过一个简单的消息的功能.RDB中思路如下: ** 在实际中可以完全借助m ...

  9. Redis 分析部分功能所解决的问题

    前言:说到缓存,大家都会想到redis,而redis中又有各种眼花缭乱的功能,今天就来看看这些功能能解决的问题. Redis官方简介 Redis是一个基于BSD开源的项目,是一个把结构化的数据放在内存 ...

随机推荐

  1. Java基础教程——打印流

    打印流 打印流可以把原本输出到控制台的信息输出到文件中.PrintStream是字节打印流(还有个对应的字符打印流是PrintWriter,这里不涉及) System类中有个变量: public fi ...

  2. Java集合【4】-- iterable和Iterator的异同分析详解

    目录 一.iterator介绍 二.iterable接口 三.为什么有Iterator还需要Iterable 一.iterator介绍 iterator接口,也是集合大家庭中的一员.和其他的Map和C ...

  3. Kafka入门之consumer

    offset存放在_consumer_offsets这个topic下 并且从0-49划分了50个分区: consumer会在kafka集群的所有broker中选择一个broker作为consumer ...

  4. Django 的模板语法之过滤器

    后端朝前端页面传递数据的方式 # 第一种 return render(request,'index.html',{'n':n}) # 第二种 return render(request,'index. ...

  5. AOV图与拓扑排序&AOE图与关键路径

    AOV网:所有的工程或者某种流程可以分为若干个小的工程或阶段,这些小的工程或阶段就称为活动.若以图中的顶点来表示活动,有向边表示活动之间的优先关系,则这样活动在顶点上的有向图称为AOV网. 拓扑排序算 ...

  6. Mac MySQL 8.0 (免安装版) 主从集群搭建

    一.下载解压包 打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/ ,选择面安装版本. 二.解压文件 下载到合适文件夹,解压压缩包. 解压 mys ...

  7. Spring Cloud 学习 (七) Spring Cloud Sleuth

    微服务架构是一个分布式架构,微服务系统按业务划分服务单元,一个微服务系统往往有很多个服务单元.由于服务单元数量众多,业务的复杂性较高,如果出现了错误和异常,很难去定位.主要体现在一个请求可能需要调用很 ...

  8. 解决IDEA更新为最新的2020.3版后,右键运行居然没有以xml形式运行的Run显示

    一.前言 个人一直喜欢用IDEA最新版,结果更新后,发现TestNg批量执行,选中testng.xml右键没Run,如下图: 刚开始以为是配置错误呢,下载了2018.2版本的IDEA,还能正常运行,于 ...

  9. 第11.11节 Python正则表达式的指定重复次数匹配模式及元字符”{}”功能介绍

    在<第11.8节 Pytho正则表达式的重复匹配模式及元字符"?". "". "+"功能介绍>和<第11.10节 Pyth ...

  10. PyQt(Python+Qt)学习随笔:Qt Designer中部件的windowTitle和windowOpacity属性

    windowOpacity 这个属性仅对window对象生效. windowOpacity为浮点数,表示透明度,为1完全不透明,为0完全透明,缺省是1. 可以通过windowOpacity().set ...