package me.zhengjie.monitor.rest; import me.zhengjie.common.aop.log.Log; import me.zhengjie.monitor.domain.vo.RedisVo; import me.zhengjie.monitor.service.RedisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; /** * @author jie * @date 2018-12-10 */ @RestController @RequestMapping("api") public class RedisController { @Autowired private RedisService redisService; @Log(description = "查询Redis缓存") @GetMapping(value = "/redis") @PreAuthorize("hasAnyRole(‘ADMIN‘,‘REDIS_ALL‘,‘REDIS_SELECT‘)") public ResponseEntity getRedis(String key, Pageable pageable){ return new ResponseEntity(redisService.findByKey(key,pageable), HttpStatus.OK); } @Log(description = "新增Redis缓存") @PostMapping(value = "/redis") @PreAuthorize("hasAnyRole(‘ADMIN‘,‘REDIS_ALL‘,‘REDIS_CREATE‘)") public ResponseEntity create(@Validated @RequestBody RedisVo resources){ redisService.save(resources); return new ResponseEntity(HttpStatus.CREATED); } @Log(description = "修改Redis缓存") @PutMapping(value = "/redis") @PreAuthorize("hasAnyRole(‘ADMIN‘,‘REDIS_ALL‘,‘REDIS_EDIT‘)") public ResponseEntity update(@Validated @RequestBody RedisVo resources){ redisService.save(resources); return new ResponseEntity(HttpStatus.NO_CONTENT); } @Log(description = "删除Redis缓存") @DeleteMapping(value = "/redis/{key}") @PreAuthorize("hasAnyRole(‘ADMIN‘,‘REDIS_ALL‘,‘REDIS_DELETE‘)") public ResponseEntity delete(@PathVariable String key){ redisService.delete(key); return new ResponseEntity(HttpStatus.OK); } @Log(description = "清空Redis缓存") @DeleteMapping(value = "/redis/all") @PreAuthorize("hasAnyRole(‘ADMIN‘,‘REDIS_ALL‘,‘REDIS_DELETE‘)") public ResponseEntity deleteAll(){ redisService.flushdb(); return new ResponseEntity(HttpStatus.OK); } }
package me.zhengjie.monitor.service.impl; import me.zhengjie.common.utils.PageUtil; import me.zhengjie.monitor.domain.vo.RedisVo; import me.zhengjie.monitor.service.RedisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import java.util.*; /** * @author jie * @date 2018-12-10 */ @Service public class RedisServiceImpl implements RedisService { @Autowired JedisPool pool; @Override public Page 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 = new RedisVo(s,jedis.get(s)); redisVos.add(redisVo); } Page<RedisVo> page = new PageImpl<RedisVo>( PageUtil.toPage(pageable.getPageNumber(),pageable.getPageSize(),redisVos), pageable, redisVos.size()); return page; }finally{ if(null != jedis){ jedis.close(); // 释放资源还给连接池 } } } @Override public void save(RedisVo redisVo) { Jedis jedis = null; try{ jedis = pool.getResource(); jedis.set(redisVo.getKey(),redisVo.getValue()); }finally{ if(null != jedis){ jedis.close(); // 释放资源还给连接池 } } } @Override public void delete(String key) { Jedis jedis = null; try{ jedis = pool.getResource(); jedis.del(key); }finally{ if(null != jedis){ jedis.close(); // 释放资源还给连接池 } } } @Override public void flushdb() { Jedis jedis = null; try{ jedis = pool.getResource(); jedis.flushDB(); }finally{ if(null != jedis){ jedis.close(); // 释放资源还给连接池 } } } }
原文地址:https://www.cnblogs.com/tonggc1668/p/11220922.html
时间: 2024-11-11 23:28:05