spring boot 整合 redis基本使用
2018-06-15 11:31
686 查看
1、 添加maven依赖
<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、 添加redis配置
本地(单点redis)
在application-local.properties(本地配置)内添加redis配置:
###redis配置### spring.redis.host=127.0.0.1 spring.redis.port=7001 spring.redis.database=0 spring.redis.password=pwd # pool settings ...池配置 spring.redis.pool.max-idle=4 spring.redis.pool.min-idle=1 spring.redis.pool.max-active=4 spring.redis.pool.max-wait=2000如果本地redis没有密码,删除spring.redis.password这行就可以了。
测试&正式环境(哨兵)
在application-dev.properties(测试环境)和application-prod.properties(正式环境)内添加redis配置:
###redis配置### spring.redis.database=0 spring.redis.password=pwd # pool settings ...池配置 spring.redis.pool.max-idle=4 spring.redis.pool.min-idle=1 spring.redis.pool.max-active=4 spring.redis.pool.max-wait=2000 #哨兵监听redis server名称 spring.redis.sentinel.master=mymaster #哨兵的配置列表 spring.redis.sentinel.nodes=host:port,host2:port2
3、使用StringRedisTemplate进行操作
注入StringRedisTemplate:@Autowired private StringRedisTemplate redisTemplate;使用RedisTemplate进行操作:
//设置缓存,建议每个键都设置过期时间 redisTemplate.opsForValue().set("test", "test", 10, TimeUnit.SECONDS); //获取缓存值 String value = redisTemplate.opsForValue().get("test"); //删除某个键 redisTemplate.delete("test"); //操作set redisTemplate.opsForSet().add("testSet", "1"); Set<String> members = redisTemplate.opsForSet().members("testSet");//获取set内的所有值 redisTemplate.opsForSet().remove("testSet", "1","2");//移除set内的多个对象 //操作list redisTemplate.opsForList().rightPush("testList", "1"); List<String> list = redisTemplate.opsForList().range("testList", 0, -1);//获取list内的所有元素StringRedisTemplate对redis操作进行了很好的封装,为键、字符串、哈希、列表、集合、有序集合、HyperLogLog的操作提供了良好的支持。基本使用形式就是redisTemplate.opsForXXX,XXX是类型,opsForValue是操作字符串;opsForSet是操作集合,opsForList是操作列表,opsForZSet是操作有序集合,opsForHyperLogLog是操作HyperLogLog,基本上其方法和redis命令是对应的,可以根据名字和方法注释快速确定方法对应的redis命令。
4、使用RedisTemplate进行操作
1、 创建RedisObjectSerializer.javaimport 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(); static final byte[] EMPTY_ARRAY = new byte[0]; public Object deserialize(byte[] bytes) { if (isEmpty(bytes)) { return null; } try { return deserializer.convert(bytes); } catch (Exception ex) { throw new SerializationException("Cannot deserialize", ex); } } 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); } }2、 配置redisTemplate
创建RedisConfig.java
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new RedisObjectSerializer()); return template; } }3、 使用RedisTemplate
//注入RedisTemplate对象 @Autowired private RedisTemplate<String, Object> objRedisTemplate; //使用RedisTemplate User user = new User(); user.setId(1); user.setName("test"); objRedisTemplate.opsForValue().set("user", user,10,TimeUnit.SECONDS); user = (User) objRedisTemplate.opsForValue().get("user");作者:思与学
链接:https://www.jianshu.com/p/8c9753c62d54
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。 阅读更多
相关文章推荐
- springboot使用protostuff进行序列化和反序列化整合redis的redisTemplate的各种方法的写法
- springboot 整合dubbo最佳实践 (使用redis作为注册中心)
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- SpringBoot学习-(二十二)SpringBoot整合Redis(使用Jedis)
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- spring boot 整合mybatis、redis、 spring mvc基本配置
- 关于springboot整合redis(使用RedisTemplate操作redis)
- 使用 SpringBoot 之 JPA 整合 Redis 实现缓存
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- Redis详解 - SpringBoot整合Redis,RedisTemplate和注解两种方式的使用
- Spring-Boot整合mybatis(三)-注解的基本使用
- springboot整合redis使用nginx实现负载均衡实现session共享
- redis学习之Jedis使用线程池封装redis的基本操作及spring的简单封装
- spring-boot 整合redis作为数据缓存
- Spring Boot使用redis做数据缓存
- Spring Boot使用redis做数据缓存
- Redis整合Spring结合使用缓存实例(三)
- 项目中使用spring和redis的整合
- Redis整合Spring结合使用缓存实例(转)