spring-boot redis配置

1.在pom.xml中引入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-redis</artifactId>
   <version>1.3.8.RELEASE</version>
</dependency>


2.在application.yml中配置参数

redis:
  host: 127.0.0.1
  port: 6379
  max-idle: 5
  max-total: 10
  max-wait-millis: 3000



3.新建一个 参数类,来 引入配置文件中的 参数,便于集中管理

@Component
@Data
public class Parameters {

//    redis config start

    @Value("${redis.host}")
    private String redisHost;
    @Value("${redis.port}")
    private int redisPort;
    @Value("${redis.max-idle}")
    private int redisMaxIdle;
    @Value("${redis.max-total}")
    private int redisMaxTotal;
    @Value("${redis.max-wait-millis}")
    private int redisMaxWaitMillis;

    //redis config  end
}

4.新建一个类,初始化链接池,提供 获取连接池的方法

@Component
@Slf4j
public class JedisPoolWrapper {
    private JedisPool jedisPool=null;

    @Autowired
    private Parameters parameters;  //注入redis的变量

    //初始化redisWrapper (PostConstruct注解相当于静态代码库,方法会在类初始化的时候 进行执行)
    @PostConstruct
    public void init() throws RedisException {
        try {
            JedisPoolConfig config=new JedisPoolConfig();
            config.setMaxIdle(parameters.getRedisMaxIdle());
            config.setMaxTotal(parameters.getRedisMaxTotal());
            config.setMaxWaitMillis(parameters.getRedisMaxWaitMillis());

            jedisPool=new JedisPool(config,parameters.getRedisHost(),parameters.getRedisPort(),2000);
        } catch (Exception e) {
            log.error("fail to initialize redis pool",e);  //记录日志
            throw new RedisException("初始化redisPool失败");   //抛出异常
        }
    }


    public JedisPool getJedisPool() {
        return jedisPool;
    }
}


5.配置一些操纵redis的方法

package com.sean.mamabike.cache;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
  *@Author sean
  *@Date 2017/10/9 11:59
  *@Description   控制redis进行 CRUD 操作的工具
 */
@Component
@Slf4j
public class CommonCacheUtils {

    @Autowired
    private JedisPoolWrapper jedisPoolWrapper;

    //1.缓存一个key(永久)
    public void cache(String key,String value){
        try {
            JedisPool jedisPool=jedisPoolWrapper.getJedisPool();
            if (jedisPool != null){
                try(Jedis jedis=jedisPool.getResource()){
                    jedis.select(0);   //选择第0号 片区 存储
                    jedis.set(key,value);
                }

            }
        } catch (Exception e) {
            log.error("fail to cache key and value",e);
        }
    }

    //2.获取redis中的缓存
    public String getCacheByKey(String key){
        String value=null;

        try {
            JedisPool jedisPool=jedisPoolWrapper.getJedisPool();  //获取链接池
            if (jedisPool != null){
                try(Jedis jedis=jedisPool.getResource()){
                    jedis.select(0);
                    value= jedis.get(key);
                }
            }
        } catch (Exception e) {
            log.error("fail to get cache",e);
        }
        return value;

    }

    //3.设置一个有缓存时间的key   SETNX:当缓存中不存在时,设置成功,返回1;已经存在,返回0
    public long cacheNxExpire(String key,String value,int expiry){
        long result=0;

        try {
            JedisPool jedisPool=jedisPoolWrapper.getJedisPool();
            if (jedisPool != null){
                try(Jedis jedis=jedisPool.getResource()){
                    jedis.select(0);
                    result=jedis.setnx(key,value);
                    jedis.expire(key,expiry);
                }
            }
        } catch (Exception e) {
            log.error("fail to cacheNxExpire",e);
        }
        return result;


    }
    //4.删除缓存
    public void deleteCache(String key){
        try {
            JedisPool jedisPool=jedisPoolWrapper.getJedisPool();

            if (jedisPool != null){
                try(Jedis jedis=jedisPool.getResource()){
                    jedis.del(key);
                }
            }
        } catch (Exception e) {
            log.error("fail to delete cache",e);
        }
    }


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值