Redis学习(3.1)spring boot整合Redis速成
2016-08-03 14:11
260 查看
本例子中,是最简略的spring boot使用redis的方式
一,pom.xml中添加
二,application.properties添加
三,创建CachingConfigurerSupport
四,使用redis
启动后测试 http://localhost:8084/redis/test?name=tianyue&no=gao
返回结果
tips:
a.User要继承Serializable接口
b.没有指定redis的key的生成规则,则默认的规则为parms的简单链接,如本例子中name=tianyue&no=gao的key为gaotianyue,但是这样的话name=otianyue&no=ga也是gaotianyue,要自定义key方式,请看3.2下一篇文章
一,pom.xml中添加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
二,application.properties添加
spring.redis.database=0 spring.redis.host=200.200.200.177 spring.redis.password=foobared spring.redis.pool.max-active=8 spring.redis.pool.max-idle=8 spring.redis.pool.max-wait=-1 spring.redis.pool.min-idle=0 spring.redis.port=6377 spring.redis.timeout=0
三,创建CachingConfigurerSupport
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport {}
四,使用redis
@Service public class RedisService { @Cacheable(value = "usercache") public User getUser(String no,String name){ LogCore.BASE.debug("invoke persistent:{},{}", no, name); return new User(no, name); } }
public class User implements Serializable { private static final long serialVersionUID = 1L; public String no; public String name; public User(String no, String name) { this.no = no; this.name = name; } }
@RestController public class RedisController { @Autowired RedisService redisService; @RequestMapping("/redis/test") public Object test(String name, String no){ String _name = Objects.toString(name, "testName"); String _no = Objects.toString(no, "testNo"); return redisService.getUser(_no, _name); } }
启动后测试 http://localhost:8084/redis/test?name=tianyue&no=gao
返回结果
{"no":"gao","name":"tianyue"}
tips:
a.User要继承Serializable接口
b.没有指定redis的key的生成规则,则默认的规则为parms的简单链接,如本例子中name=tianyue&no=gao的key为gaotianyue,但是这样的话name=otianyue&no=ga也是gaotianyue,要自定义key方式,请看3.2下一篇文章
相关文章推荐
- redis学习系列(二)--spring boot整合Redis集群
- SpringBoot(三)整合Redis
- spring-boot整合redis 总结
- Spring Boot 整合 Redis 实现缓存操作
- spring-boot 速成(6) 整合disconf
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- springboot 整合Redis
- spring-boot整合redis作为缓存(3)——自定义key
- Springboot整合Redis
- Spring Boot 整合 Redis 实现缓存操作
- spring boot整合redis主从sentinel
- spring boot整合redis实现缓存机制
- springboot整合spring-data-redis中的坑
- spring-boot 整合redis作为数据缓存
- spring-boot 整合redis作为数据缓存
- SpringBoot 整合 Redis 的简单案例
- 【spring boot】SpringBoot初学(8)– 简单整合redis
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- springboot整合spring-data-redis遇到的坑