spring boot redis template 使用

由于之前的项目是spring 项目 然后改造成 spring boot 项目 所以   redis 还是用的 RedisTemplate

话不多说直接上代码。

首先pom文件  redis maven:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

然后 spring boot  yml 文件中的参数:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password:
    pool:
      maxIdle: 300
      maxActive: 600
      maxWait: 1000
      testOnBorrow: true

 

然后是redis config:

package com.qaq.configuation;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @auther: QAQ
 * @date: 2018-12-17 
 * @description: redis缓存配置
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,T> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setStringSerializer(new StringRedisSerializer());
        redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setEnableDefaultSerializer(true);
        redisTemplate.setEnableTransactionSupport(false);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

这时候就能使用redisTemplate 了

直接  

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

不过我一般在项目中 包装一下:

 

package com.qaq.cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

@Component
public class RedisCacheManager<T> {

    @Autowired
    private RedisTemplate<String, T> redisTemplate;
    private static final Logger LOG = LogManager.getLogger(RedisCacheManager.class);

    @Value("${redisDefaultRxpire}")
    private Long redisDefaultRxpire;

    public void put(String key, T data, long expire) throws Exception{
        try{
            redisTemplate.boundValueOps(key).set(data, expire, TimeUnit.MILLISECONDS);

        }catch(Exception e){
            LOG.error("REDIS-ERROR:" + e.getMessage());
            throw new Exception(e);
        }
    }

    public T get(String key) {
        try{
            return redisTemplate.boundValueOps(key).get();
        }catch(Exception e){
            LOG.error("REDIS-ERROR:" + e.getMessage(),e);
        }
        return null;
    }

    public void remove(String key) {
        redisTemplate.delete(key);
    }
    public void remove(List<String> keys) throws Exception{
        redisTemplate.delete(keys);
    }

    public void setRedisTemplate(RedisTemplate<String, T> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public RedisTemplate<String, T> getRedisTemplate() {
        return redisTemplate;
    }

    public Long getRedisDefaultRxpire() {
        return redisDefaultRxpire;
    }

    public void setRedisDefaultRxpire(Long redisDefaultRxpire) {
        this.redisDefaultRxpire = redisDefaultRxpire;
    }
}

然后service 中就能愉快的使用了:

    @Autowired
    protected RedisCacheManager<Object> redisCacheManager;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值