由于之前的项目是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;