Springboot整合Redis
2017-08-23 14:28
357 查看
windows的redis版本下载:http://download.csdn.net/download/a295277302/9940194
源码路径:http://download.csdn.net/download/a295277302/9946554
登陆是admin/admin
添加redis依赖jar
指明版本号是因为本文所用的springboot版本为1.5.4
如果不指明版本号会出现unknown的问题
查找原因是因为redis到目前的最高版本为1.4.7.RELEASE
所以才会找不到版本号。
之后添加RedisConfig
到目前为止 Redis缓存已经添加成功了。
测试:
字符串缓存
对象缓存
注解缓存
目前本地缓存为:
添加之后:
源码路径:http://download.csdn.net/download/a295277302/9946554
登陆是admin/admin
添加redis依赖jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency>
指明版本号是因为本文所用的springboot版本为1.5.4
如果不指明版本号会出现unknown的问题
查找原因是因为redis到目前的最高版本为1.4.7.RELEASE
所以才会找不到版本号。
之后添加RedisConfig
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport{ @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @SuppressWarnings("rawtypes") @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager rcm = new RedisCacheManager(redisTemplate); //设置缓存过期时间 //rcm.setDefaultExpiration(60);//秒 return rcm; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
到目前为止 Redis缓存已经添加成功了。
测试:
@Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate;
字符串缓存
stringRedisTemplate.opsForValue().set("aaa", "111");
对象缓存
ser user=new User("aa@126.com", "aa", "aa123456", "aa","123"); ValueOperations<String, User> operations=redisTemplate.opsForValue(); operations.set("com.neox", user); operations.set("com.neo.f", user,1,TimeUnit.SECONDS); Thread.sleep(1000); //redisTemplate.delete("com.neo.f"); boolean exists=redisTemplate.hasKey("com.neo.f"); if(exists){ System.out.println("exists is true"); }else{ System.out.println("exists is false"); }
注解缓存
@Cacheable(value="user-key") public UserResponse getUserInfo(@PathVariable String id){ UserResponse user = userService.findById(id); return user; }
目前本地缓存为:
添加之后:
相关文章推荐
- spring boot整合redis主从sentinel
- springboot学习笔记-3 整合redis&mongodb【转载】
- springboot+shiro+redis(单机redis版)整合教程
- springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
- springboot整合redis,实现session共享
- springboot使用protostuff进行序列化和反序列化整合redis的redisTemplate的各种方法的写法
- springboot整合redis
- SpringBoot+Redis+Mybatis+AngularJS整合开发
- SpringBoot之整合redis实现缓存
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- spring-boot | 整合Redis缓存数据
- SpringBoot整合redis哨兵主从服务
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- Springboot中Spring-cache与redis整合
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- spring boot整合redis(单机+集群)
- spring boot整合redis实现shiro的分布式session共享
- springboot整合redis
- springboot整合spring-data-redis遇到的坑