packageme.zhengjie.monitor.rest;
importme.zhengjie.common.aop.log.Log;
importme.zhengjie.monitor.domain.vo.RedisVo;
importme.zhengjie.monitor.service.RedisService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.data.domain.Pageable;
importorg.springframework.http.HttpStatus;
importorg.springframework.http.ResponseEntity;
importorg.springframework.security.access.prepost.PreAuthorize;
importorg.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
* @authorjie
* @date 2018-12-10
*/
@RestController
@RequestMapping("api")
public classRedisController {
@Autowired
privateRedisService redisService;
@Log(description = "查询Redis缓存")
@GetMapping(value = "/redis")
@PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_SELECT')")
publicResponseEntity getRedis(String key, Pageable pageable){
return newResponseEntity(redisService.findByKey(key,pageable), HttpStatus.OK);
}
@Log(description = "新增Redis缓存")
@PostMapping(value = "/redis")
@PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_CREATE')")
publicResponseEntity create(@Validated @RequestBody RedisVo resources){
redisService.save(resources);
return newResponseEntity(HttpStatus.CREATED);
}
@Log(description = "修改Redis缓存")
@PutMapping(value = "/redis")
@PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_EDIT')")
publicResponseEntity update(@Validated @RequestBody RedisVo resources){
redisService.save(resources);
return newResponseEntity(HttpStatus.NO_CONTENT);
}
@Log(description = "删除Redis缓存")
@DeleteMapping(value = "/redis/{key}")
@PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_DELETE')")
publicResponseEntity delete(@PathVariable String key){
redisService.delete(key);
return newResponseEntity(HttpStatus.OK);
}
@Log(description = "清空Redis缓存")
@DeleteMapping(value = "/redis/all")
@PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_DELETE')")
publicResponseEntity deleteAll(){
redisService.flushdb();
return newResponseEntity(HttpStatus.OK);
}
}
packageme.zhengjie.monitor.service.impl;
importme.zhengjie.common.utils.PageUtil;
importme.zhengjie.monitor.domain.vo.RedisVo;
importme.zhengjie.monitor.service.RedisService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.data.domain.Page;
importorg.springframework.data.domain.PageImpl;
importorg.springframework.data.domain.Pageable;
importorg.springframework.stereotype.Service;
importredis.clients.jedis.Jedis;
importredis.clients.jedis.JedisPool;
import java.util.*;
/**
* @authorjie
* @date 2018-12-10
*/
@Service
public class RedisServiceImpl implementsRedisService {
@Autowired
JedisPool pool;
@Override
publicPage findByKey(String key, Pageable pageable){
Jedis jedis = null;
try{
jedis =pool.getResource();
List<RedisVo> redisVos = new ArrayList<>();
if(!key.equals("*")){
key = "*" + key + "*";
}
for(String s : jedis.keys(key)) {
RedisVo redisVo = newRedisVo(s,jedis.get(s));
redisVos.add(redisVo);
}
Page<RedisVo> page = new PageImpl<RedisVo>(
PageUtil.toPage(pageable.getPageNumber(),pageable.getPageSize(),redisVos),
pageable,
redisVos.size());
returnpage;
}finally{
if(null !=jedis){
jedis.close(); //释放资源还给连接池
}
}
}
@Override
public voidsave(RedisVo redisVo) {
Jedis jedis = null;
try{
jedis =pool.getResource();
jedis.set(redisVo.getKey(),redisVo.getValue());
}finally{
if(null !=jedis){
jedis.close(); //释放资源还给连接池
}
}
}
@Override
public voiddelete(String key) {
Jedis jedis = null;
try{
jedis =pool.getResource();
jedis.del(key);
}finally{
if(null !=jedis){
jedis.close(); //释放资源还给连接池
}
}
}
@Override
public voidflushdb() {
Jedis jedis = null;
try{
jedis =pool.getResource();
jedis.flushDB();
}finally{
if(null !=jedis){
jedis.close(); //释放资源还给连接池
}
}
}
}