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. java使用mongoTemplate去重排序查询

    import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.c ...

  2. Tensorflow学习教程------简单练一波,线性模型

    #coding:utf-8 import tensorflow as tf import numpy as np #使用numpy 生成100个随机点 x_data = np.random.rand( ...

  3. AD软件将PCB中的元器件旋转45度

  4. 网格视图GridView

    1.常用属性 2.Adapter接口 3.Demo演示 今天观看了GridView的相关视频,并且根据案例,进行了代码的编写和实例 新建GridViewActivity.java继承AppCompat ...

  5. Vue动态添加v-model绑定及获取其返回数据

    从数据库拿到的动态数据绑定到页面对应的v-model或者v-bind上,并且根据对页的操作获取到返回的值: 1.首先在data里定义一个数据 timeTip 为一个空数组 data () { retu ...

  6. (day 1)创建项目--1

    1.利用cmd(命令行)创建项目myblog 确定好项目要放在哪个directory. dir一下创建好的项目看下有什么 django自带有一个小型的服务器可通过  runserver 启动它 可取浏 ...

  7. WOJ 1542 Countries 并查集转化新点+最短路

    http://acm.whu.edu.cn/land/problem/detail?problem_id=1542 今天做武大的网赛题,哎,还是不够努力啊,只搞出三个 这个题目一看就是个最短路,但是题 ...

  8. Windows如何设置指定的IP走专线?

    很多时候在工作中难免有多重网络环境的情况,为了方便之间的互访,可能会用的VPN等虚拟专线,作为网络管理员,route命令是必会的基础技能. 我们一般连接到专线vpn以后,默认会启用远程网关,这样我们所 ...

  9. 元祖&字典

    #什么是元祖:元祖是一个不可变的列表(没有改的需求) #======================================基本使用============================== ...

  10. 我的第一次JAVA实训——校园公用房管理系统

    老铁们,昨天电脑没电了.这是上周答应的大项目. 别打脸. 详细内容我之后再写,下面是代码地址. Github地址 附:一个寂寞 以上