redis连接方式
//心跳刷新缓存
$redis = $this->container->get(\Redis::class);
//获取所有的客户端id
$fdList = $redis->sMembers('websocket_sjd_1');
//如果当前客户端在客户端集合中,就刷新
if (in_array($frame->fd, $fdList)) {
$redis->sAdd('websocket_sjd_1', $frame->fd);
$redis->expire('websocket_sjd_1', 7200);
}
$server->push($frame->fd, 'Recv: ' . $frame->data.json_encode($fdList));
redis 代理连接池
composer require hyperf/redis #安装
实际是继承了redis客户端代理封装成连接池
<?php
use Hyperf\Utils\ApplicationContext;
$container = ApplicationContext::getContainer();
$redis = $container->get(Hyperf\Redis\Redis::class);
$result = $redis->keys('*');
多个连接的redis
$container = ApplicationContext::getContainer();
$container=$this->container;
$redis = $container->get(RedisFactory::class)->get('foo');
$redis->set('foo',1);
网友评论