Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

This section describes the code for the CacheController section of the upload and download module in ruoyi-Vue’s Ruoyi-admin module. This interface is mainly used to show the Redis server

Server status

You can use the redis info command to return redis information and statistics directly

Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info());
Copy the code

Redis command statistics

Adding CommandStats to the info command allows the command to return only a portion of the information

Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));
Copy the code

A strong cast is performed on the returned data in order to do something about the returned command. The returned data format is

cmdstat_XXX: calls=XXX,usec=XXX,usec_per_call=XXX
Copy the code

So the following code processes the returned data into an array of PIE charts.

        List<Map<String, String>> pieList = new ArrayList<>();
        commandStats.stringPropertyNames().forEach(key -> {
            Map<String, String> data = new HashMap<>(2);
            String property = commandStats.getProperty(key);
            data.put("name", StringUtils.removeStart(key, "cmdstat_"));
            data.put("value", StringUtils.substringBetween(property, "calls=".",usec"));
            pieList.add(data);
        });
        result.put("commandStats", pieList);
Copy the code

DBSIZE

The DBSIZE command in Redis returns the number of keys in the current database.

Object dbSize = redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize());
Copy the code

This one returns Object, but both of the preceding ones change the return strength to Properties because it’s only a single number, which is returned directly to the front end.

Information used by the front end

The title Corresponds to the variable name returned
Redis version cache.info.redis_version
port cache.info.tcp_port
The client number cache.info.connected_clients
Running time (days) cache.info.uptime_in_days
The use of the CPU parseFloat(cache.info.used_cpu_user_children).toFixed(2)
Memory configuration cache.info.maxmemory_human
The Key number cache.info.maxmemory_human
Memory configuration cache.dbSize
Network entrance/exit {{ cache.info.instantaneous_input_kbps }}kps/{{cache.info.instantaneous_output_kbps}}kps

This interface is very simple, is to provide a front-end page to feed back some information to the site owner, mainly info command and DBsize command is usually not accessible, can be recorded as Redis knowledge points.