我有同樣的問題,照以下步驟解決:
1.安裝 https://github.com/Bokt/flarum-redis[ext] 包
2.加上以下自建Class
use Predis\Client;
/**
* \Redis
*/
class Redis
{
const CONFIG_FILE = '../redis.php';
protected static $redis;
public static function init()
{
self::$redis = new Client([
'host' => '127.0.0.1',
'port' => 6379
]);
}
public static function set($key,$value,$time=null,$unit=null)
{
self::init();
if ($time) {
switch ($unit) {
case 'h':
$time *= 3600;
break;
case 'm':
$time *= 60;
break;
case 's':
case 'ms':
break;
default:
throw new InvalidArgumentException('单位只能是 h m s ms');
break;
}
if ($unit=='ms') {
self::_psetex($key,$value,$time);
} else {
self::_setex($key,$value,$time);
}
} else {
self::$redis->set($key,$value);
}
}
public static function get($key)
{
self::init();
return self::$redis->get($key);
}
public static function delete($key)
{
self::init();
return self::$redis->del($key);
}
private static function _setex($key,$value,$time)
{
self::$redis->setex($key,$time,$value);
}
private static function _psetex($key,$value,$time)
{
self::$redis->psetex($key,$time,$value);
}
}
3.
Redis::set('key','value',5,'s');
echo Redis::get('key');
即可使用