<?php
class TestRedis
{
const ADD_SCORE = 432;
const EXPIRATION_TIME = 86400 * 7;
private $redis;
public function __construct()
{
$this->redis = new Redis();
$this->redis->connect('127.0.0.1', 6379);
}
public function publishArticle($authorId, $tag)
{
$articleId = $this->getId('article:id');
$tagId = $this->getId('tag:id');
$articleContent = [
'title' => 'article title' . $articleId,
'author' => 'Rebort' . $articleId,
'content' => 'hello word',
'tag' => $tag,
];
$contentResult = $this->redis->hMset("article:{$articleId}:content", $articleContent);
$timeResult = $this->redis->zAdd("article:time", time(), $articleId);
$scoreResult = $this->redis->zAdd("article:score", time(), $articleId);
$userResult = $this->redis->sAdd("article:{$articleId}:vote", $authorId);
$expireResult = $this->redis->expire("article:{$articleId}:vote", self::EXPIRATION_TIME);
$tagResult = $this->saveArticleTag($tag, $tagId);
$userResult = $this->redis->sAdd("{$tag}:{$tagId}", $articleId);
if ($articleId && $contentResult && $timeResult && $scoreResult && $userResult
&& $expireResult && $tagResult) {
return '釋出文章成功';
} else {
return '釋出文章失敗';
}
}
public function saveArticleTag($tag, $tagId)
{
$exists = $this->redis->sismember('article:tags', $tag . ':' . $tagId);
if (!$exists) {
return $this->redis->sAdd('article:tags', $tag . ':' . $tagId);
}
}
public function showArticlesOfReleaseTime($start, $end)
{
$result = $this->redis->zRevRange('article:time', $start, $end, true);
return $this->joinArticle($result);
}
public function showArticlesOfLikeNumber($start, $end)
{
$result = $this->redis->zRevRange('article:score', $start, $end, true);
return $this->joinArticle($result);
}
public function joinArticle($articles)
{
$result = '';
if (!empty($articles)) {
foreach ($articles as $articleId => $value) {
$articleHashKey = 'article:' . $articleId . ':content';
$articleInfo = $this->redis->hGetAll($articleHashKey);
$result .=
'<div>'.
'<span>文章 id:' . $articleId . ' </span>' .
'<span>文章標題:'. $articleInfo['title'] . ' </span>' .
'<span>文章內容:'. $articleInfo['content'] . ' </span>' .
'<span>文章作者:' . $articleInfo['author'] . ' </span>' .
'<span>文章標籤:' . $articleInfo['tag'] . ' </span>' .
'</div>';
}
} else {
$result = '還未發表文章';
}
return $result;
}
public function giveLike($userId, $articleId)
{
$publishTime = $this->redis->zScore('article:time', $articleId);
$isOverdue = (time() - $publishTime) < self::EXPIRATION_TIME;
$isClick = $this->isGiveLike($userId, $articleId);
if (!empty($publishTime) && $isOverdue && !$isClick) {
$addUserResult = $this->redis->sAdd("article:{$articleId}", $userId);
$addScoreResult = $this->redis->zIncrBy('article:score', self::ADD_SCORE, $articleId);
return ($addUserResult && $addScoreResult) ? '點贊成功' : '點贊失敗';
}
return '點贊失敗';
}
public function isGiveLike($userId, $articleId)
{
$isClick = $this->redis->sismember("article:{$articleId}", $userId);
if ($isClick) {
$result = true;
} else {
$result = false;
}
return $result;
}
public function getId($key, $stepSize = 1)
{
return $this->redis->incrBy($key, $stepSize);
}
}
$obj = new TestRedis();
var_dump($obj->publishArticle(999, 'Redis'));
本作品採用《CC 協議》,轉載必須註明作者和本文連結