新增Redis终端管理,修复菜单管理删除失败的BUG

This commit is contained in:
郑杰
2018-09-22 13:14:45 +08:00
parent b3ec2e51df
commit cba2e9fba9
14 changed files with 792 additions and 50 deletions
@@ -0,0 +1,157 @@
package me.aurora.app.rest.system;
import me.aurora.annotation.Log;
import me.aurora.domain.ResponseEntity;
import me.aurora.service.RedisService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.Set;
/**
* @author 郑杰
* @date 2018/09/22 10:48:11
*/
@RestController
@RequestMapping("redis")
public class RedisController {
private static final String INTEGER_PREFIX = "(integer)";
@Autowired
private RedisService redisService;
@GetMapping("terminal")
@RequiresPermissions (value={"admin", "redis:all","redis:terminal"}, logical= Logical.OR)
public ModelAndView redisTerminal(Model model) {
String osName = System.getProperty("os.name");
model.addAttribute("osName", osName);
return new ModelAndView("/system/redis/terminal");
}
@Log("执行Redis keys命令")
@RequestMapping("keys")
public ResponseEntity keys(String arg) {
try {
Set<String> set = this.redisService.getKeys(arg);
return ResponseEntity.ok(set);
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行Redis get命令")
@GetMapping("get")
public ResponseEntity get(String arg) {
try {
String result = this.redisService.get(arg);
return ResponseEntity.ok(result == null ? "" : result);
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行Redis set命令")
@GetMapping("set")
public ResponseEntity set(String arg) {
try {
String[] args = arg.split(",");
if (args.length == 1){
return ResponseEntity.error("(error) ERR wrong number of arguments for 'set' command");
} else if(args.length != 2){
return ResponseEntity.error("(error) ERR syntax error");
}
String result = this.redisService.set(args[0], args[1]);
return ResponseEntity.ok(result);
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行Redis del命令")
@GetMapping("del")
public ResponseEntity del(String arg) {
try {
String[] args = arg.split(",");
Long result = this.redisService.del(args);
return ResponseEntity.ok(INTEGER_PREFIX + result);
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行Redis exists命令")
@GetMapping("exists")
public ResponseEntity exists(String arg) {
try {
int count = 0;
String[] arr = arg.split(",");
for (String key : arr) {
if (this.redisService.exists(key)){
count++;
}
}
return ResponseEntity.ok(INTEGER_PREFIX + count);
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行Redis pttl命令")
@GetMapping("pttl")
public ResponseEntity pttl(String arg) {
try {
return ResponseEntity.ok(INTEGER_PREFIX + this.redisService.pttl(arg));
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行Redis pexpire命令")
@GetMapping("pexpire")
public ResponseEntity pexpire(String arg) {
try {
String[] arr = arg.split(",");
if (arr.length != 2 || !isValidLong(arr[1])) {
return ResponseEntity.error("(error) ERR wrong number of arguments for 'pexpire' command");
}
return ResponseEntity.ok(INTEGER_PREFIX + this.redisService.pexpire(arr[0], Long.valueOf(arr[1])));
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行flushdb命令")
@GetMapping("flushdb")
public ResponseEntity flushdb() {
try {
return ResponseEntity.ok(INTEGER_PREFIX + this.redisService.flushdb());
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
@Log("执行flushall命令")
@GetMapping("flushall")
public ResponseEntity flushall() {
try {
return ResponseEntity.ok(INTEGER_PREFIX + this.redisService.flushall());
} catch (Exception e) {
return ResponseEntity.error(e.getMessage());
}
}
private static boolean isValidLong(String str) {
try {
Long.parseLong(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
@@ -3,6 +3,7 @@ package me.aurora.config.redis;
import afu.org.checkerframework.checker.oigj.qual.O;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
@@ -21,6 +22,9 @@ import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSeriali
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
@@ -67,6 +71,18 @@ public class RedisConfig extends CachingConfigurerSupport {
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build());
}
@Bean
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(Long.parseLong(maxWaitMillis.replace("ms","")));
if (StringUtils.isNotBlank(password)) {
return new JedisPool(jedisPoolConfig, host, port, Integer.parseInt(timeout.replace("ms","")), password);
} else {
return new JedisPool(jedisPoolConfig, host, port, Integer.parseInt(timeout.replace("ms","")));
}
}
/**
* 序列化
* @param redisConnectionFactory
@@ -29,6 +29,13 @@ public class ResponseEntity extends HashMap<String, Object>{
return responseEntity;
}
public static ResponseEntity ok(Object msg) {
ResponseEntity responseBo = new ResponseEntity();
responseBo.put("code", 0);
responseBo.put("msg", msg);
return responseBo;
}
public static ResponseEntity ok(String msg) {
ResponseEntity responseEntity = new ResponseEntity();
responseEntity.put("msg", msg);
@@ -1,8 +1,6 @@
package me.aurora.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@@ -12,8 +10,7 @@ import java.util.Date;
* @author 郑杰
* @date 2018/09/21 15:38:06
*/
@Getter
@Setter
@Data
public class UserOnline implements Serializable{
/**
@@ -0,0 +1,79 @@
package me.aurora.service;
import java.util.Set;
/**
* @author 郑杰
* @date 2018/09/22 10:42:52
*/
public interface RedisService {
/**
* 获取 key
* @param pattern 正则
* @return Set
*/
Set<String> getKeys(String pattern);
/**
* get命令
*
* @param key key
* @return String
*/
String get(String key);
/**
* set命令
*
* @param key key
* @param value value
* @return String
*/
String set(String key, String value);
/**
* del命令
*
* @param key key
* @return Long
*/
Long del(String... key);
/**
* exists命令
*
* @param key key
* @return Boolean
*/
Boolean exists(String key);
/**
* pttl命令
*
* @param key key
* @return Long
*/
Long pttl(String key);
/**
* pexpire命令
*
* @param key key
* @param milliscends 毫秒
* @return Long
*/
Long pexpire(String key, Long milliscends);
/**
* flushdb 命令
* @return
*/
String flushdb();
/**
* flushall 命令
* @return
*/
String flushall();
}
@@ -19,6 +19,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author 郑杰
@@ -100,12 +101,14 @@ public class MenuServiceImpl implements MenuService {
@Override
public void delete(Menu menu) {
List<Menu> menus = menuRepo.findByPid(menu.getPid());
if(menus !=null && menus.size()!=0){
menuRepo.deleteAll(menus);
if(menu.getLevelNum() == 0){
menuRepo.delete(menu);
} else {
Set<Menu> menus = menuRepo.findByPid(menu.getPid()).stream().collect(Collectors.toSet());
if(menus !=null && menus.size()!=0){
menuRepo.deleteAll(menus);
}
}
menuRepo.delete(menu);
}
@Override
@@ -0,0 +1,81 @@
package me.aurora.service.impl;
import me.aurora.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.*;
import java.util.function.Function;
/**
* Redis 工具类,只封装了几个常用的 redis 命令,
* 可根据实际需要按类似的方式扩展即可。
* @author 郑杰
* @date 2018/09/22 10:42:52
*/
@Service("redisService")
@SuppressWarnings("unchecked")
public class RedisServiceImpl implements RedisService {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
JedisPool jedisPool;
/**
* 处理jedis请求
* @param f 处理逻辑,通过lambda行为参数化
* @return 处理结果
*/
private Object excuteByJedis(Function<Jedis, Object> f) {
try (Jedis jedis = jedisPool.getResource()) {
return f.apply(jedis);
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
@Override
public Set<String> getKeys(String pattern) {
return (Set<String>) this.excuteByJedis(j -> j.keys(pattern));
}
@Override
public String get(String key) {
return (String) this.excuteByJedis(j -> j.get(key));
}
@Override
public String set(String key, String value) {
return (String) this.excuteByJedis(j -> j.set(key, value));
}
@Override
public Long del(String... key) {
return (Long) this.excuteByJedis(j -> j.del(key));
}
@Override
public Boolean exists(String key) {
return (Boolean) this.excuteByJedis(j -> j.exists(key));
}
@Override
public Long pttl(String key) {
return (Long) this.excuteByJedis(j -> j.pttl(key));
}
@Override
public Long pexpire(String key, Long milliseconds) { return (Long) this.excuteByJedis(j -> j.pexpire(key, milliseconds)); }
@Override
public String flushdb() { return (String) this.excuteByJedis(j -> j.flushDB()); }
@Override
public String flushall() { return (String) this.excuteByJedis(j -> j.flushAll()); }
}