博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot 整合redis --sea 方式1
阅读量:4485 次
发布时间:2019-06-08

本文共 4452 字,大约阅读时间需要 14 分钟。

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 配置方式
@Beanpublic CacheManager cacheManager(RedisTemplate redisTemplate) {    RedisCacheManager cacheManager= new RedisCacheManager(redisTemplate);    cacheManager.setDefaultExpiration(60);    Map
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@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport {    @Bean    public CacheManager cacheManager(RedisTemplate
redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(1800); return cacheManager; } @Bean public RedisTemplate
redisTemplate(RedisConnectionFactory factory) { RedisTemplate
template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new RedisObjectSerializer()); return template; }}
View Code
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 {    private Converter
serializer = new SerializingConverter(); private Converter
deserializer = new DeserializingConverter(); private static final byte[] EMPTY_ARRAY = new byte[0]; @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 == 0); }}
View Code

 

 

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"));    }

 

转载于:https://www.cnblogs.com/lshan/p/10271308.html

你可能感兴趣的文章
(转)python3之模块io使用流的核心工具
查看>>
阶乘模版
查看>>
ShellShock 攻击实验
查看>>
BAT及各大互联网公司前端笔试面试题--Html,Css篇
查看>>
Linux下的时间戳
查看>>
xpath的学习
查看>>
kvm系列之四:热添加技术
查看>>
grep命令
查看>>
powershell的stable和preview版本
查看>>
DateTime
查看>>
火狐浏览器设置bypass
查看>>
XMLHttpRequest 对象
查看>>
C语言中的循环结构与选择结构
查看>>
加锁解锁PHP实现 -转载
查看>>
java Object类的公共方法
查看>>
floodlight make the VMs can not getDHCP IP address
查看>>
利用unittest+ddt进行接口测试(二):使用yaml文件管理测试数据
查看>>
11 个创新的网站滑动效果设计案例展示
查看>>
BZOJ4675
查看>>
闭包、循环setTimeout、立即执行函数
查看>>