Redis学习(3.1)spring boot整合Redis速成

31 篇文章 0 订阅
27 篇文章 0 订阅

本例子中,是最简略的spring boot使用redis的方式

一,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下一篇文章


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值