All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xin.altitude.cms.monitor.controller.CacheController Maven / Gradle / Ivy

There is a newer version: 1.3.4.1
Show newest version
package xin.altitude.cms.monitor.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.altitude.cms.common.entity.AjaxResult;
import xin.altitude.cms.framework.config.CmsConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 缓存监控
 *
 * @author ucode
 */
@ResponseBody
@RequestMapping(CmsConfig.UNIFORM_PREFIX + "/monitor/cache")
public class CacheController {
    @Autowired
    private RedisTemplate redisTemplate;
    
    // @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping()
    public AjaxResult getInfo() throws Exception {
        Properties info = (Properties) redisTemplate.execute((RedisCallback) RedisServerCommands::info);
        Properties commandStats = (Properties) redisTemplate.execute((RedisCallback) connection -> connection.info("commandstats"));
        Object dbSize = redisTemplate.execute((RedisCallback) RedisServerCommands::dbSize);
        
        Map result = new HashMap<>(3);
        result.put("info", info);
        result.put("dbSize", dbSize);
        
        List> pieList = new ArrayList<>();
        commandStats.stringPropertyNames().forEach(key -> {
            Map 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);
        return AjaxResult.success(result);
    }
}