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(); // 释放资源还给连接池
}
} }
}

查询Redis缓存的更多相关文章

  1. redis缓存和mysql数据库同步

    附redis关于缓存雪崩和缓存穿透,热点key 穿透 穿透:频繁查询一个不存在的数据,由于缓存不命中,每次都要查询持久层.从而失去缓存的意义. 解决办法: 持久层查询不到就缓存空结果,查询时先判断缓存 ...

  2. [技术博客] 用户验证码验证机制---redis缓存数据库的使用

    目录 问题引入 初识redis 实际应用 作者:马振亚 问题引入 在这次的开发过程中,我们的需求中有一个是普通用户可以通过特定的机制申请成为社长.因为只有部分人才能验证成功,所以这个最开始想了两种思路 ...

  3. Java SpringBoot使用Redis缓存和Ehcache

    <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...

  4. 关于redis缓存数据库的一些思考

    今晚无聊,躺在床上,在刷技术文章时,看见了一篇关于redis缓存的文章 写的蛮好,这也就引起了我对于redis思考! 不如往深了说 引起了我对于追求探索技术本质的一些思考 平时在网上刷到很多关于red ...

  5. 本地缓存,Redis缓存,数据库DB查询(结合代码分析)

    问题背景 为什么要使用缓存?本地缓存/Redis缓存/数据库查询优先级? 一.为什么要使用缓存 原因:CPU的速度远远高于磁盘IO的速度问题:很多信息存在数据库当中的,每次查询数据库就是一次IO操作所 ...

  6. [转]在nodejs使用Redis缓存和查询数据及Session持久化(Express)

    本文转自:https://blog.csdn.net/wellway/article/details/76176760 在之前的这篇文章 在ExpressJS(NodeJS)中设置二级域名跨域共享Co ...

  7. 在nodejs使用Redis缓存和查询数据及Session持久化(Express)

    在nodejs使用Redis缓存和查询数据及Session持久化(Express) https://segmentfault.com/a/1190000002488971

  8. 结合redis缓存的方式,查询和展示分类信息

    package cn.itcast.travel.service.impl;import cn.itcast.travel.dao.CategoryDao;import cn.itcast.trave ...

  9. 总结:如何使用redis缓存加索引处理数据库百万级并发

    前言:事先说明:在实际应用中这种做法设计需要各位读者自己设计,本文只提供一种思想.准备工作:安装后本地数redis服务器,使用mysql数据库,事先插入1000万条数据,可以参考我之前的文章插入数据, ...

随机推荐

  1. POJ 2186:Popular Cows Tarjan模板题

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25945   Accepted: 10612 De ...

  2. POJ 1035:Spell checker

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22574   Accepted: 8231 De ...

  3. POST和GET方法乱码解决方案

    前言 在WEB开发的过程中,中文乱码是最为常见的问题之一.之所以会出现中文乱码的情况,主要原因是:前端使用POST或者GET方法传递的参数一般使用浏览器预先设置的编码方式进行编码,中文浏览器一般是使用 ...

  4. clion 的 安装 变量配置的 搬运工(有点基础应该能看 大家看不懂 就是我自己看 哈哈哈哈哈哈)

    1  自行安装一个  clion 2 https://blog.csdn.net/u013023297/article/details/80723847   mingw  的配置    第二篇好像当时 ...

  5. STL库中的equal_range()

    equal_range根据键值,返回一对迭代器的pair对象.如果该键值在容器中存在,则pair对象中的第一个迭代器指向该键关联的第一个实例,第二个迭代器指向该键关联的最后一个实例的下一位置.如果找不 ...

  6. 修改maven默认仓库(即repository)的路径

    原文链接:https://blog.csdn.net/ideality_hunter/article/details/53006188 简要说明:主要操作为新建仓库路径,在maven的conf目录下修 ...

  7. UML-迭代2:更多模式

    1.之前的初始阶段+细化阶段中的迭代1讲述的是广泛使用的基本分析和对象设计技术.而迭代2中,案例研究只强调: 对象设计和模式: 1).基本对象设计(基于职责和GRASP) 2).使用模式来创建稳固的设 ...

  8. javaweb06 文件的下载

    1. 如何修改小工具或框架的源代码 ? 1). 原则: 能不修改就不修改. 2). 修改的方法: > 修改源代码, 替换 jar 包中对应的 class 文件. > 在本地新建相同的包, ...

  9. ubuntu服务器上配置tomcat

    前言 嗯,最近想在自己的腾讯云服务器上跑个项目玩玩,由于服务器是重装的系统,所以,只能自己手动装tomcat. 不过,tomcat是基于java的,必须又java环境tomcat才能够使用,因此首先要 ...

  10. @Autowired的几个使用细节

    1.使用@Autowired的当前类也必须由spring容器托管(打@Coponent.@Controller.@Service .@repository) 2.不管是public 和  privat ...