基于spring.version:4.x;
4.2.4.RELEASE
不同spring版本對應(yīng)的redis版本也不一樣找默,配置類也會不同。
添加pom:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
redis參數(shù)配置:RedisConfig.class
package com.chenglulu.api.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class RedisConfig {
@Value("127.0.0.1")
private String redisHost;
@Value("3306")
private Integer redisPort;
@Value("123456")
private String redisPassword;
}
redis配置類:RedisConfiguration.class
package com.chenglulu.api;
import com.chenglulu.api.config.RedisConfig;
import com.chenglulu.util.CryptorUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class RedisConfiguration {
@Autowired
private RedisConfig redisConfig;
private static final Logger logger = LoggerFactory.getLogger(RedisConfiguration.class);
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
/*最大空閑數(shù)*/
config.setMaxIdle(50);
/*連接池的最大數(shù)據(jù)庫連接數(shù)*/
config.setMaxTotal(30000);
/*最大建立連接等待時間*/
config.setMaxWaitMillis(100);
/*是否在從池中取出連接前進(jìn)行檢驗,如果檢驗失敗,則從池中去除連接并嘗試取出另一個*/
config.setTestOnBorrow(true);
/*在空閑時檢查有效性, 默認(rèn)false*/
config.setTestWhileIdle(false);
return config;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
String password = redisConfig.getRedisPassword();
JedisConnectionFactory factory = new JedisConnectionFactory(jedisPoolConfig());
factory.setHostName(redisConfig.getRedisHost());
factory.setPort(redisConfig.getRedisPort());
factory.setPassword(password);
factory.setTimeout(100);
factory.setDatabase(1);
return factory;
}
@Bean(name = "redisTemplate")
public StringRedisTemplate initRedisTemplate() {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(jedisConnectionFactory());
// 自定義Redis序列化器
// RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
// RedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 設(shè)置序列化器
// stringRedisTemplate.setKeySerializer(stringRedisSerializer);
// stringRedisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
// stringRedisTemplate.setHashKeySerializer(stringRedisSerializer);
// stringRedisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
// stringRedisTemplate.setEnableTransactionSupport(true);
return stringRedisTemplate;
}
@Bean(name = "redisCacheManager")
public CacheManager initRedisCacheManager(StringRedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// 設(shè)置超時時間為10分鐘晃酒,單位為秒
cacheManager.setDefaultExpiration(600);
// 設(shè)置緩存名稱
List<String> cacheNames = new ArrayList<>();
cacheNames.add("redisCacheManager");
// cacheManager.setCacheNames(cacheNames);
return cacheManager;
}
}
redis工具類:redisUtils.class
package com.chenglulu.api.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisUtil {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
public StringRedisTemplate getStringRedisTemplate() {
return this.stringRedisTemplate;
}
public RedisConnection getRedisConnection() {
return this.stringRedisTemplate.getConnectionFactory().getConnection();
}
// 是否存在key
public Boolean hasKey(String key) {
return stringRedisTemplate.hasKey(key);
}
// 設(shè)置指定key的值
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
// 獲取指定key的值
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
}
示例使用
@Service
public class redisService {
@Autowired
private RedisUtil redisUtil;
public void setTestRedis(){
RedisConnection redisConnection = getRedisConnection();
try{
redisUtil.set("test","123456");
}catch(Exception e){
}
if(redisConnection != null){
redisConnection.close();
}
}
public String getTestRedis(){
RedisConnection redisConnection = getRedisConnection();
String value = null;
try{
if(redisUtil.hasKey("test")){
value = redisUtil.get("test");
};
}catch(Exception e){
}
if(redisConnection != null){
redisConnection.close();
}
return value;
}
/*判斷redis是否可用*/
public RedisConnection getRedisConnection(){
RedisConnection redisConnection = null;
try {
redisConnection = redisUtil.getRedisConnection();
}catch (RedisConnectionFailureException e){
logger.error("getRedisConnection RedisConnectionFailureException e = {}", e);
}catch (Exception e){
logger.error("getRedisConnection Exception e = {}", e);
}
if(redisConnection == null){
//可以在redis有異常時做一些事情
}
logger.info("getRedisConnection RedisConnection is = {}", redisConnection);
return redisConnection;
}
}