spring boot使用总结(四)在spring boot中使用redis
2016-11-27 16:20
591 查看
1) 想要在spring boot中使用redis,只要在pom文件中增加如下的配置就可以了。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2) 引入spring-boot-redis jar包之后,选择相应的客户端配置连接就可以使用redis了,推荐使用Jedis。
@Bean JedisConnectionFactory connectionFactory() { return new JedisConnectionFactory(); }
3)配置完连接后,就可以使用redis进行简单的存取了。如果存取的key,value都是String的,可以使用
RedisTemplate<String, String>对象,该对象由spring boot自动扫描生成,在需要的地方可以直接注入。
如果只是进行简单的存储,还可以生成ValueOperations对象来方便操作。
@Bean ValueOperations<String, String> strOperations(RedisTemplate<String, String> redisTemplate) { return redisTemplate.opsForValue(); }
4)如果想要存取其他类型的对象,例如存取key为string,value为Integer类型的对象,可以new这个对象, 然后配置连接即可使用。
@Bean RedisTemplate<String, Integer> intRedisTemplate(JedisConnectionFactory connectionFactory) { RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>(); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; }
5)RedisTemplate对象默认使用jdkSerializer实现序列化,如果想要更换序列化的实现方式,例如使用json实现 value的序列化,可以进行如下配置
@Bean Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( Object.class); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); return jackson2JsonRedisSerializer; } @Bean RedisTemplate<String, Object> objRedisTemplate(JedisConnectionFactory connectionFactory, Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(connectionFactory); redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); return redisTemplate; }
最后附上上述操作的完整代码
@Configuration public class RedisConfig { @Bean JedisConnectionFactory connectionFactory() { return new JedisConnectionFactory(); } @Bean ValueOperations<String, String> strOperations(RedisTemplate<String, String> redisTemplate) { return redisTemplate.opsForValue(); } @Bean RedisTemplate<String, Integer> intRedisTemplate(JedisConnectionFactory connectionFactory) { RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>(); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } @Bean ValueOperations<String, Integer> intOperations(RedisTemplate<String, Integer> redisTemplate) { return redisTemplate.opsForValue(); } @Bean Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( Object.class); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); return jackson2JsonRedisSerializer; } @Bean RedisTemplate<String, Object> objRedisTemplate(JedisConnectionFactory connectionFactory, Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(connectionFactory); redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); return redisTemplate; } @Bean ValueOperations<String, Object> objOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } }
相关文章推荐
- Redis系列三 - Spring boot如何使用redis做缓存及缓存注解的用法总结
- Redis系列三 - Spring boot如何使用redis做缓存及缓存注解的用法总结
- Redis系列三 - Spring boot如何使用redis做缓存及缓存注解的用法总结
- springboot结合redis使用CachingConfigurerSupport方法不能被继承
- spring boot(三):Spring Boot中Redis的使用
- 【Spring Boot&&Spring Cloud系列】Spring Boot中使用NoSql数据库Redis
- spring boot使用redis
- 1 Springboot中使用redis,自动缓存、更新、删除
- Spring Boot使用redis做数据缓存
- spring boot Redis使用
- REDIS学习(3.2)spring boot 使用redis作为缓存
- spring boot(三):Spring Boot中Redis的使用
- [spring-boot] 使用redis
- Redis学习总结(2)——Redis整合Spring结合使用缓存实例
- springboot(三):Spring boot中Redis的使用
- Spring Boot中使用Redis数据库
- Spring Boot中Redis的使用
- Spring Boot中使用Redis数据库
- MyBatis-Spring-Boot 使用总结
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存