最近遇到个这样的需求:点赞排行榜,在规定的时间内(可能有好几天),谁获赞多谁就获胜。今天试了下redis消息订阅,发现还可以,写写具体过程吧。
1.安装redis,phpredis扩展
这些网上一搜一大把
2.搜了个redis类,新建类:app\common\controller\MyRedis,代码如下:
<?php
namespace app\common\controller;
class MyRedis
{
private $redis;
public function __construct($host = '127.0.0.1', $port = 6379)
{
$this->redis = new \Redis();
$this->redis->config('notify-keyspace-events','Ex');//开启redis key 过期通知(改过配置文件,但没成功)
$this->redis->connect($host, $port);
}
public function setex($key, $time, $val)
{
return $this->redis->setex($key, $time, $val);
}
public function set($key, $val)
{
return $this->redis->set($key, $val);
}
public function get($key)
{
return $thi