产品价值

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. H3CNE(教程)

    培训机构提供的ppt,可能也是来自于官方提供,涉及到H3CNE认证考试中的全部知识点,学真技术还得看这个.包括帧中继,哪怕是淘汰了十多年了. https://huxiaoyao.lanzous.com ...

  2. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

  3. springmvc跨域问题

    1.跨域问题: 按照网上所有的方法试了一遍,都没跨过去,正在无助之际,使用filter按照下面的方法解决的时候出现了转机: 添加filter: package com.thc.bpm.filter; ...

  4. java并发编程实战《七》安全性、活跃性以及性能问题

    安全性.活跃性以及性能问题 安全性问题 那什么是线程安全呢?其实本质上就是正确性,而正确性的含义就是程序按照我们期望的执行,不要让我们感到意外. 存在共享数据并且该数据会发生变化,通俗地讲就是有多个线 ...

  5. moviepy音视频开发:使用credits1给视频加片头片尾字幕

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:视频基类VideoClip子类DataVideoClip.UpdatedVideoClip.ImageClip. ...

  6. 第8.16节 Python重写自定义类的__str__方法

    一. 引言 上节结合案例介绍了重写__repr__方法的关注点,重写__repr__方法的要点是要准确的输出开发人员关注的信息,并便于开发人员使用相关信息.而__str__方法是为最终用户返回类的相关 ...

  7. PyQt(Python+Qt)学习随笔:containers容器部件GroupBox分组框介绍

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.主要属性 GroupBox分组框是一个对多个部件进行编组的框架容器,可以带有标题(title属性 ...

  8. PyQt学习随笔:Model/View设计中支持视图中数据修改的方法及步骤

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 要支持视图中的数据可以修改,需要两个步骤: 1. 在视图中设置editTriggers属性支持在视图 ...

  9. 半夜删你代码队 Day6冲刺

    一.每日站立式会议 1.站立式会议 成员 昨日完成工作 今日计划工作 遇到的困难 陈惠霖 完成注册界面 好友界面 无 侯晓龙 了解数据库使用 帮助他人建立数据库 无 周楚池 完成登录界面+管理员界面初 ...

  10. element ui只输入数字校验

    注意:圈起来的两个地方,刚开始忘记写type='number'了,导致可以输入'123abc'这样的,之后加上了就OK了