Redis缓存内存状态

时间:2019-06-27 05:06:39

标签: spring spring-boot redis

我需要创建一个页面,在其中可以显示Redis缓存的内存状态,例如已分配的内存量和正在使用的内存量。使用spring boot有什么办法可以获取所有这些信息吗?

2 个答案:

答案 0 :(得分:2)

是的,有可能。我告诉你如何使用Redis-client: Jedis

在项目中添加jedis依赖项后,您可以定义一个bean,如:

    @Bean
    public JedisConnectionFactory connectionFactory() {
        return new JedisConnectionFactory();            
        //in-case if you have a cluster
        //return new JedisConnectionFactory(new RedisClusterConfiguration(Arrays.asList("127.0.0.1:6379,127.0.0.1:6369".split(","))));
    }

现在您可以像这样使用它:

class DummyClass{
    @Autowired
    JedisConnectionFactory factory;

    void printServerInfo(){
      Properties info = factory.getConnection().info()
      System.out.println(info)
    }
}

这是一个非常基本的示例,说明如何在 spring-boot 中使用jedis获取服务器信息。 这是一个示例输出,其中包含的信息:希望能满足您有关服务器的所有信息。

[  
   uptime_in_seconds:6,
   maxmemory_human:0   B,
   aof_last_cow_size:0,
   master_replid2:0000000000000000000000000000000000000000,
   mem_replication_backlog:0,
   aof_rewrite_scheduled:0,
   total_net_input_bytes:14,
   rss_overhead_ratio:1.36,
   hz:10,
   redis_build_id:5e26944   cbfa127d5,
   aof_last_bgrewrite_status:ok,
   multiplexing_api:epoll,
   client_recent_max_output_buffer:0,
   allocator_resident:3657728,
   mem_fragmentation_bytes:4156584,
   repl_backlog_first_byte_offset:0,
   redis_mode:standalone,
   redis_git_dirty:1,
   allocator_rss_bytes:2510848,
   repl_backlog_histlen:0,
   rss_overhead_bytes:1310720,
   total_system_memory:16679944192,
   loading:0,
   evicted_keys:0,
   cluster_enabled:0,
   redis_version:5.0   .3,
   repl_backlog_active:0,
   mem_aof_buffer:0,
   allocator_frag_bytes:160352,
   instantaneous_ops_per_sec:0,
   used_memory_human:834.11   K,
   role:master,
   maxmemory:0,
   used_memory_lua:37888,
   rdb_current_bgsave_time_sec:-1,
   used_memory_startup:790976,
   lazyfree_pending_objects:0,
   used_memory_dataset_perc:21.31   %,
   allocator_frag_ratio:1.16,
   arch_bits:64,
   mem_clients_normal:49694,
   expired_time_cap_reached_count:0,
   mem_fragmentation_ratio:6.12,
   aof_last_rewrite_time_sec:-1,
   master_replid:95   dcc7b8b38c04621f8cbc089ce6e13c82d3ca58,
   aof_rewrite_in_progress:0,
   config_file:,
   lru_clock:1356079,
   maxmemory_policy:noeviction,
   run_id:8   bfe7f1cebd97ff0dfb98436e76084db6c781010,
   latest_fork_usec:0,
   total_commands_processed:0,
   expired_keys:0,
   used_memory:854128,
   mem_clients_slaves:0,
   keyspace_misses:0,
   executable:/redis-server,
   used_memory_peak_human:834.11   K,
   keyspace_hits:0,
   rdb_last_cow_size:0,
   used_memory_overhead:840670,
   active_defrag_hits:0,
   tcp_port:6379,
   uptime_in_days:0,
   used_memory_peak_perc:105.21   %,
   blocked_clients:0,
   sync_partial_err:0,
   used_memory_scripts_human:0   B,
   aof_current_rewrite_time_sec:-1,
   aof_enabled:0,
   master_repl_offset:0,
   used_memory_dataset:13458,
   used_cpu_user:0.011536,
   rdb_last_bgsave_status:ok,
   atomicvar_api:atomic-builtin,
   allocator_rss_ratio:3.19,
   client_recent_max_input_buffer:4,
   aof_last_write_status:ok,
   mem_allocator:jemalloc-5.1.0,
   used_memory_scripts:0,
   used_memory_peak:854128,
   process_id:9682,
   used_cpu_sys:0.007885,
   repl_backlog_size:1048576,
   connected_slaves:0,
   gcc_version:7.3   .0,
   total_system_memory_human:15.53   G,
   sync_full:0,
   connected_clients:1,
   allocator_active:1146880,
   total_net_output_bytes:0,
   pubsub_channels:0,
   active_defrag_key_hits:0,
   rdb_changes_since_last_save:0,
   instantaneous_input_kbps:0.00,
   configured_hz:10,
   used_memory_rss_human:4.74   M,
   expired_stale_perc:0.00,
   active_defrag_misses:0,
   used_cpu_sys_children:0.000000,
   number_of_cached_scripts:0,
   sync_partial_ok:0,
   used_memory_lua_human:37.00   K,
   rdb_last_save_time:1561637161,
   pubsub_patterns:0,
   slave_expires_tracked_keys:0,
   redis_git_sha1:9101   cbde,
   used_memory_rss:4968448,
   rdb_last_bgsave_time_sec:-1,
   os:Linux 4.15.0-52-generic x86_64,
   mem_not_counted_for_evict:0,
   active_defrag_running:0,
   rejected_connections:0,
   active_defrag_key_misses:0,
   allocator_allocated:986528,
   instantaneous_output_kbps:0.00,
   second_repl_offset:-1,
   rdb_bgsave_in_progress:0,
   used_cpu_user_children:0.000000,
   total_connections_received:1,
   migrate_cached_sockets:0
]

答案 1 :(得分:1)

签出RedisClient

这是一个GUI,可以完成您尝试做的事情。根据自述文件,他们使用 Jedis 。我没有完全检查他的代码。但是他使用

redis.clients.jedis.Tuple