application.properties

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
 

Springboot 2.x 版本 RedisCacheManager 类的配置,【与 1.x 略有不同】

1、1.x 配置方式
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager= new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(60);
Map<String,Long> expiresMap=new HashMap<>();
expiresMap.put("Product",5L);
cacheManager.setExpires(expiresMap);
return cacheManager;
}
2、2.x 配置方式
    @Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}

config:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport { @Bean
public CacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration();
return cacheManager;
} @Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
package com.goku.demo.config;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
private static final byte[] EMPTY_ARRAY = new byte[]; @Override
public Object deserialize(byte[] bytes) {
if (isEmpty(bytes)) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
} @Override
public byte[] serialize(Object object) {
if (object == null) {
return EMPTY_ARRAY;
}
try {
return serializer.convert(object);
} catch (Exception ex) {
return EMPTY_ARRAY;
}
} private boolean isEmpty(byte[] data) {
return (data == null || data.length == );
}
}

test:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class TestRedis implements Serializable{ private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired
private RedisTemplate redisTemplate; @Test
public void test() throws Exception {
// 保存字符串
//redisTemplate.opsForValue().set("sea", "111");
this.logger.info((String) redisTemplate.opsForValue().get("sea"));
}

最新文章

  1. 15个关于Chrome的开发必备小技巧[译]
  2. Struts2文件上传和文件下载
  3. Ubuntu下deb包的安装方法 (zz)
  4. python del 注意点
  5. Lake Counting_深度搜索_递归
  6. rsync.conf详解
  7. linux内核分析——扒开系统调用的三层皮
  8. POJ 3150 Cellular Automaton --矩阵快速幂及优化
  9. Secret and Whisper
  10. 第3章 抽象工厂模式(Abstract Factory)
  11. 浅谈JavaScript递归
  12. POJ [P3660] Cow Contest
  13. Swift基础之集成单选按钮横竖两种样式
  14. BATCH+VBS脚本自动执行命令
  15. 常用Common集合
  16. 3.SLB 回话保持功能分析
  17. rlwrap与历史命令
  18. CSS技巧收集——毛玻璃效果
  19. VS2015 打包winform 安装程序
  20. C/C++ 下mysql应用封装(连接增删改查)

热门文章

  1. 解决js输出汉字乱码问题
  2. 关于Javascript闭包(Closure)
  3. Notepad++ 中使用tail -f功能
  4. python3学习笔记三(数字类型,字符串)
  5. css-去掉IE浏览器自带&#215;号
  6. [译] OpenStack Ocata 版本中的 53 个新功能盘点
  7. 学习笔记之TensorFlow
  8. 廖雪峰Java2面向对象编程-6Java核心类-6常用工具类
  9. Underscore.js(1.9.1) 封装库
  10. error while loading shared libraries: xxx.so