Heim  >  Artikel  >  Backend-Entwicklung  >  So implementieren Sie die Follow-Funktion in PHP

So implementieren Sie die Follow-Funktion in PHP

藏色散人
藏色散人Original
2021-11-19 09:34:122196Durchsuche

So implementieren Sie die Aufmerksamkeitsfunktion in PHP: 1. Erstellen Sie den Implementierungscode der Kontrollschicht „Namespace AppControllerTest...“; 2. Entwerfen Sie den Implementierungscode der Serviceschicht „Namespace AppServicePtg...“; Code.

So implementieren Sie die Follow-Funktion in PHP

Die Betriebsumgebung dieses Artikels: Windows 7-System, PHP-Version 7.1, DELL G3-Computer

Wie implementiert man die Follow-Funktion in PHP?

php + Redis implementiert die Follow-Funktion:

Produktwert

1: Follow-Funktion
2: Funktionsanalyse der „Follow“-Funktion
3: Hinter der gewöhnlichen „Follow“-Funktion stehen 4 wichtige Punkte Wert

Anwendungsszenarien

Bei der Arbeit am PC oder in der APP können einige soziale Konzepte mit Follow- und Fan-Funktionen kombiniert werden;
Wenn die Datenmenge gering ist, kann die Datenbank sie dennoch unterstützen riesig, es ist besser, Caching zu verwenden.

Spezifische Implementierung

1 Control-Layer-Implementierung

<?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 Service-Layer-Implementierung

<?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 = &#39;follow&#39;, int $userId, int $otherId)
    {
        // 关注
        if ($type === &#39;follow&#39;) {
            // 先处理 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;
    }
}
?>

Warehouse-Layer-Implementierung [Empfohlen: PHP-Video-Tutorial]

<?php

namespace App\Repository\Redis;

class TestFollowRedis extends AbstractRedis
{
    /**
     * 关注key
     * @var string
     */
    private $followKey = &#39;%u:follow&#39;;

    /**
     * 粉丝key
     * @var string
     */
    private $fansKey = &#39;%u:fans&#39;;

    /**
     * 前缀
     */
    public function initPrefix()
    {
        return &#39;follow:&#39;;
    }

    /**
     * 增加关注
     * @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);
    }
}

Das obige ist der detaillierte Inhalt vonSo implementieren Sie die Follow-Funktion in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn