0

0

php怎么实现关注功能

藏色散人

藏色散人

发布时间:2021-11-19 09:34:12

|

2546人浏览过

|

来源于php中文网

原创

php实现关注功能的方法:1、创建控制层实现代码“namespace app\controller\test...”;2、设计服务层实现代码“namespace app\service\ptg...”;3、设置好仓储层代码即可。

php怎么实现关注功能

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php怎么实现关注功能?

php + redis 实现关注功能:

产品价值

1: 关注功能
2: 功能分析之“关注”功能
3: 平平无奇的「关注」功能,背后有4点重大价值

立即学习PHP免费学习笔记(深入)”;

ecshop微信商城接口
ecshop微信商城接口

ecshop微信商城接口,实现商品融合,关注注册,发放优惠券,ecshop分销以及微信支付,微信收货地址共享等强大功能.

下载

应用场景

在做PC或者APP端时,掺杂点社交概念就有关注和粉丝功能;
数据量小的话数据库还能支持,如果数据量很庞大还是用缓存比较好。

具体实现

1 控制层实现

<?php

namespace App\Controller\Test;

use App\Controller\AbstractController;
use App\Service\Ptg\TestFollowService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * 测试 - 关注
 * Class TestFollowController
 * @package App\Controller
 * @Controller(prefix="test")
 */
class TestFollowController extends AbstractController
{
    /**
     * 服务层 - 关注
     * @Inject()
     * @var TestFollowService
     */
    protected $testFollowService;

    /**
     * 关注/取消关注
     * @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 服务层实现

<?php

namespace App\Service\Ptg;

use App\Repository\Redis\TestFollowRedis;
use App\Service\AbstractService;
use Hyperf\Di\Annotation\Inject;

class TestFollowService extends AbstractService
{
    /**
     * 仓储层 - 关注
     * @Inject()
     * @var TestFollowRedis
     */
    protected $testFollowRedis;

    /**
     * 关注/取消关注
     * @param string $type
     * @param int $userId
     * @param int $otherId
     * @return mixed
     */
    public function follow($type = 'follow', int $userId, int $otherId)
    {
        // 关注
        if ($type === 'follow') {
            // 先处理 mysql
            // TODO mysql 操作
            // 然后处理 redis
            $this->testFollowRedis->zAddFollow($userId, $otherId);
            $this->testFollowRedis->zAddFans($otherId, $userId);
        }
        // 取消关注
        if ($type === 'remove') {
            // 先处理 mysql
            // TODO mysql 操作
            // 然后处理 redis
            $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视频教程

<?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
     */
    public function zAddFollow($userId, $otherId)
    {
        $this->redis->zAdd(sprintf($this->prefix . $this->followKey, $userId), time(), $otherId);
    }

    /**
     * 取消关注
     * @param $userId
     * @param $otherId
     */
    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
     */
    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
     */
    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
     */
    public function zAddFans($userId, $otherId)
    {
        $this->redis->zAdd(sprintf($this->prefix . $this->fansKey, $userId), time(), $otherId);
    }

    /**
     * 移除粉丝
     * @param $userId
     * @param $otherId
     */
    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
     */
    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
     */
    public function zRevRangeFans(int $userId, int $start = 0, int $end = 9)
    {
        return $this->redis->zRevRange(sprintf($this->prefix . $this->fansKey, $userId), $start, $end);
    }
}

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
常用的数据库软件
常用的数据库软件

常用的数据库软件有MySQL、Oracle、SQL Server、PostgreSQL、MongoDB、Redis、Cassandra、Hadoop、Spark和Amazon DynamoDB。更多关于数据库软件的内容详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1003

2023.11.02

内存数据库有哪些
内存数据库有哪些

内存数据库有Redis、Memcached、Apache Ignite、VoltDB、TimesTen、H2 Database、Aerospike、Oracle TimesTen In-Memory Database、SAP HANA和ache Cassandra。更多关于内存数据库相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

666

2023.11.14

mongodb和redis哪个读取速度快
mongodb和redis哪个读取速度快

redis 的读取速度比 mongodb 更快。原因包括:1. redis 使用简单的键值存储,而 mongodb 存储 json 格式的数据,需要解析和反序列化。2. redis 使用哈希表快速查找数据,而 mongodb 使用 b-tree 索引。因此,redis 在需要高性能读取操作的应用程序中是一个更好的选择。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

501

2024.04.02

redis怎么做缓存服务器
redis怎么做缓存服务器

redis 作为缓存服务器的答案:redis 是一款开源、高性能、分布式的键值存储,可作为缓存服务器使用。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

412

2024.04.07

redis怎么解决数据一致性
redis怎么解决数据一致性

redis 提供了两种一致性模型,以维护副本数据一致性:强一致性 (sync) 确保写操作仅在复制到所有从节点后才完成;最终一致性 (async) 则在主节点上写操作后认为已完成,牺牲一致性换取性能。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

407

2024.04.07

mysql和redis怎么保证双写一致性
mysql和redis怎么保证双写一致性

确保 mysql 和 redis 双写一致性的技术包括:1、事务性更新:同时更新 mysql 和 redis,保证一致性;2、主从复制:mysql 主服务器更改同步到 redis 从服务器;3、基于事件的更新:mysql 记录更改并发送到 redis等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

476

2024.04.07

redis缓存一般存些什么数据
redis缓存一般存些什么数据

redis缓存中存储的数据类型包括:字符串、哈希、列表、集合、有序集合、位图、地理空间数据和hyperloglog。这些数据类型适用于存储各种数据,从简单信息到复杂对象和地理位置。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

424

2024.04.07

redis的8种数据类型有哪些
redis的8种数据类型有哪些

redis 提供 8 种数据类型:字符串(文本、数字、二进制)、哈希(键值对)、列表(有序集合)、集合(无序唯一元素)、有序集合(按分数排序)、地理空间(地理位置)、hyperloglog(估计大数据基数)和位图(位序列存储)。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

448

2024.04.07

Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

4

2026.03.05

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 13万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 11.3万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号