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);