redission查看Redis内存使用情况

在使用Redisson的过程中,发现该框架没有提供对应的方法供我们查看redis的内存使用情况。

网上大部分都是使用的jedis,对于redission的资料倒是比较少。

无奈下查阅了官方文档,发现需要使用原生的命令去调用。

通过redission预先写好的一些原生调用方式去调用。代码如下:

对于没有数据库密码的redis还需要额外判断一下,以免org.redisson.client.RedisClient类的对象创建失败

   public  Map<String,String> getMemoryInfo(){
        RedisConnection conn = getMRedisClient().connect();
        Map<String,String> memoryInfo = conn.sync(StringCodec.INSTANCE, RedisCommands.INFO_MEMORY);
        conn.closeAsync();
        return  memoryInfo;
    }
    private org.redisson.client.RedisClient mRedisClient;
    public org.redisson.client.RedisClient getMRedisClient(){
        if(mRedisClient == null) {
            EventLoopGroup group = new NioEventLoopGroup();
            RedisClientConfig config = new RedisClientConfig();
            config.setAddress(globalProperties.redis_url);
            if(!StringUtils.isEmpty(globalProperties.redis_pass)){
                config.setPassword(globalProperties.redis_pass);
            };
            config.setDatabase(0)
                    .setClientName("myClient")
                    .setGroup(group);
            mRedisClient = org.redisson.client.RedisClient.create(config);
        }
        return mRedisClient;
    }

效果如下:

猜你喜欢

转载自blog.csdn.net/weixin_42547157/article/details/82631100