查询Redis缓存

摘要:
=jedis){jedis.clo
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(); //释放资源还给连接池
}
        }
    }
}

免责声明:文章转载自《查询Redis缓存》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇软件测试之支付模块测试ESlint中console.log报错问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

redis开机自动启动服务

一、首先是下载Redis(这里我选择64位)github下载地址:https://github.com/MicrosoftArchive/redis/releases 二、解压压缩包,进入安装目录在空白处按下shift+鼠标右键,打开命令窗口。 三、输入命令执行:redis-server --service-install redis.windows.co...

jedis 连接 redis

一、连接单机版的 redis /** * 直接连接 redis * @throws Exception */ @Test public void test1() throws Exception { //创建一个 jedis 对象,参数:host、post Jedis jedis = new Jedis("192.168.25.128...

redis 系列22 复制Replication (下)

一. 复制环境准备   1.1 主库环境(172.168.18.201) 环境 说明 操作系统版本 CentOS  7.4.1708  IP地址 172.168.18.201 网关Gateway 172.168.18.1 DNS 172.168.16.11 Redis版本和端口 4.0.6  和 6379 Redis 密码 12...

redis 系列16 持久化 RDB

一.概述   Redis是内存数据库,一旦服务器进程退出,服务器中的数据库内存数据状态也会消失。为了解决这个问题,Redis提供了RDB 持久化功能,这个功能可以将redis在内存中的数据库状态保存到磁盘中,避免数据意外丢失。   RDB持久化可以手动执行,也可以根据服务器配置选项定期执行,是在指定的时间间隔,对你的数据进行快照存储。该RDB文件快照是一个...

C# 通过ServiceStack 操作Redis——Hash类型的使用及示例

接着上一篇,下面转到hash类型的代码使用 Hash:结构 key-key-value,通过索引快速定位到指定元素的,可直接修改某个字段 /// <summary> /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直接修改某个字段 /// str...

Jedis

使用Jedis 需要把jedis依赖的jar包添加到工程中。Maven工程中需要把jedis的坐标添加到依赖。 1.1. 连接单机版 第一步:创建一个Jedis对象。需要指定服务端的ip及端口。 第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。 第三步:打印结果。 第四步:关闭Jedis 1 @Test 2 public void t...