查询Redis缓存

Posted tonggc1668

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询Redis缓存相关的知识,希望对你有一定的参考价值。

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缓存的主要内容,如果未能解决你的问题,请参考以下文章

php 分页查询怎么redis缓存

redis做mysql的缓存

SpringAOP与Redis搭建缓存

查询数据放入了redis中缓存,怎么查看缓存的数据

Redis缓存穿透 解析

Redis缓存雪崩缓存穿透缓存击穿