美文网首页phphyperf
hyperf遇到的坑redis连接报错

hyperf遇到的坑redis连接报错

作者: yichen_china | 来源:发表于2020-05-03 18:39 被阅读0次

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

    相关文章

      网友评论

        本文标题:hyperf遇到的坑redis连接报错

        本文链接:https://www.haomeiwen.com/subject/ngkjghtx.html