~~~
<?php
/**
* @Author: 陳靜
* @Date: 2018/05/29 07:33:49
* @Description:
*/
namespace app\redis\controller;
use Predis\Client;
use think\Controller;
class Tools extends Controller
{
public $oneWeekInSecond = 604800;
public $voteScore = 432;
public $redis = '';
public function __construct()
{
$redisConfig = config('redis');
$this->redis = new Client($redisConfig);
}
public function index()
{
//發布文章
// $this->postArticle(1, 'test1', 'test1');
// $this->postArticle(1, 'test2', 'test2');
// $this->postArticle(1, 'test3', 'test3');
//對文章投票
// $article['id'] = 1;
// $article['name'] = 'article:1';
// $this->articleVote(2,$article);
// $this->articleVote(3,$article,0);
//獲取文章
// $article = $this->getArticles(1);
// halt($article);
}
//發布文章
public function postArticle($user,$title,$link)
{
//生成文章id
$articleId = $this->redis->incr('article:');
//將發布者添加到已投票的用戶組去
$voted = 'voted:'.$articleId;
$this->redis->sadd($voted,$user);
$this->redis->expire($voted,$this->oneWeekInSecond);
$article = 'article:'.$articleId;
$this->redis->hmset($article,[
'title' => $title,
'link' => $link,
'poster' => $user,
'time' => time(),
'votes' => 1,
'opvotes' => 0
]);
$this->redis->zadd('score:',[$article => $this->voteScore]);
$this->redis->zadd('time:',[$article =>time()]);
return $articleId;
}
//用戶投票功能
public function articleVote($user_id,$article,$type = 1)
{
$publishTime = $this->redis->zscore('time:',$article['name']);
//獲取文章發布時間(超過一周不再允許投票)
if ($publishTime + $this->oneWeekInSecond < time()) {
return;
}
//投贊成票
if ($type == 1){
$result = $this->redis->sadd('voted:'.$article['id'],$user_id);
if ($result) {
$this->redis->zincrby('score:',$this->voteScore,$article['name']);
$this->redis->hincrby($article['name'],'votes',1);
}
}else{
//實現反對票
//1。檢查用戶是否投過贊成票
$hasVoted = $this->redis->sismember('voted:'.$article['id'],$user_id);
if ($hasVoted) {
return;
}
$result = $this->redis->sadd('OpVoted:',$user_id);
if ($result) {
$this->redis->zincrby('score:',-$this->voteScore,$article['name']);
$this->redis->hincrby($article['name'],'opvotes',1);
}
}
}
//獲取文章
public $articlePerPage = 25;
public function getArticles($page,$order = 'score:')
{
$start = ($page - 1) * $this->articlePerPage;
$end = $start + $this->articlePerPage - 1;
$ids = $this->redis->zrevrange($order,$start,$end);
$articles = [];
foreach ($ids as $id){
$articleData = $this->redis->hgetall($id);
$articleData['id'] = $id;
$articles[] = $articleData;
}
return $articles;
}
//添加或者刪除文章分組
public function addOrRemoveGroups($articleId,array $toAdd,array $toRemove)
{
$article = 'article:'.$articleId;
foreach ($toAdd as $group){
$this->redis->sadd('group:'.$group,$article);
}
foreach ($toRemove as $group){
$this->redis->srem('group:'.$group,$article);
}
}
//獲取組內的文章
public function getGroupArticles($group,$page,$order = 'score:'){
$key = $order.$group;
if ($this->redis->exists($key)) {
$this->redis->zinterstore($key,['group:'.$group,$order],['aggregate' => 'max']);
$this->redis->expire($key,60);
}
return $this->getArticles($page,$key);
}
}
~~~
- PHP7新特性
- 優雅的寫代碼
- 常見的代碼優化
- 常用的工具類
- PHP原生生成EXCEL
- PHP地理位置計算
- PHP獲取服務器狀態
- 駝峰轉下劃線
- 百度地圖兩點坐標距離計算
- 判斷是否是url
- PHP常見header頭
- 郵件發送類
- 阿拉伯數字轉化為大寫
- 獲取漢字首個拼音
- 根據身份證號獲取星座
- 生成驗證碼類
- 生成唯一ID
- 身份證驗證類
- PHP中文轉拼音
- Nginx配置文件
- curl獲取網頁內容
- 快遞查詢api
- 上傳圖片類
- 股票類
- 找回密碼類
- 字符串助手函數
- 校驗數據規則
- PHP獲取收集相關信息
- 字符串截取助手函數
- 網頁中提取關鍵字
- 檢測瀏覽器語言
- 微信相關類
- 微信獲取access_token
- 獲取用戶基本信息
- 代碼規范
- 編程規范(psr-1,2)
- 編程規范(原作者的建議)
- 經驗
- 常用函數地址
- 函數集合
- 一些常識
- MYSQL相關知識
- 常用sql
- mysql事務隔離級別
- Read uncommitted
- Read committed
- Repeatable read
- Serializable
- 高性能MYSQL讀書筆記
- 第一章MYSQL的架構
- mysql邏輯架構
- redis相關知識
- 1.安裝redis
- 3.php操作redis
- 隊列
- 悲觀鎖
- 樂觀鎖
- 發布
- 訂閱
- redis實戰-文章投票
- 設計模式
- 創建模型實例
- 單例模式
- 工廠模式
- AnimalInterface.php
- Chicken.php
- Factory.php
- Farm.php
- Pig
- SampleFactory.php
- Zoo
- 抽象工廠模式
- AnimalFactory
- Factory
- FarmInterface
- Income
- PandaZoo
- PeonyZoo
- PigFarm
- PlantFactory
- RiceFarm
- ZooInterface
- 原型模式
- 建造者模式
- 結構型模式實例
- 橋接模式
- 享元模式
- 外觀模式
- 適配器模式
- 裝飾器模式
- 組合模式
- 代理模式哦
- 過濾器模式
- 行為型模式實例
- 模板模式
- 策略模式
- 狀態模式
- 觀察者模式
- 責任鏈模式
- 訪問者模式
- 解釋器模式
- 空對象模式
- 中介者模式
- 迭代器模式
- 命令模式
- 備忘錄模式
- 網絡知識
- 互聯網協議概述
- nginx簡易交互過程
- HTTP知識
- LINUX相關知識
- swoole學習
- 1.初識swoole
- 2.WebSocket PHP 即時通訊開發
- 3.異步多進程的 CURL
- 4.異步非阻塞多進程的 Http 服務器
- 5.TCP 服務器
- 5.1同步 TCP 客戶端
- 5.2異步 TCP 客戶端
- 6.UDP 服務器
- 7.異步多任務處理
- 8.毫秒定時器
- 9.高并發投票
- ThinkPHP5學習
- 命令行操作
- 所有命令行中用到的基類
- 1.base
- 2.WorkerBase
- 3.TimeWorkerBase
- 4.CycleWorkerBase
- 5.WorkerCommandBase
- 6.WorkerHookBase
- 1.基礎命令實現
- 2.建立Linux上的守護源碼
- 3.發送模板消息
- 4.基于命令行實現自己的隊列模式
- 5.發送定時短信
- thinkphp5使用sentry
- sentry通知,記錄日志
- 高級查詢
- Kafka相關
- 1.安裝
- 2.為php打擴展
- 3.kafka實現
- 一些工具搭建
- sentry日志收集系統搭建
- walle搭建
- php實現定時任務
- 檢測文件變化