1.在pom中配置
<dependency>
<groupId>cn.gjing</groupId>
<artifactId>tools-redis</artifactId>
<version>1.0.0</version>
</dependency>
2.在application.properties配置
#redis数据库链接配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
3.在DemoApplication.java上配置
@EnableSecondCache
4.编写DemoController
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping("demo")
public boolean demo(@RequestParam("id")Integer id){
return demoService.demo(id);
}
}
5.编写DemoService
@Service
public class DemoService {
/**
* 当id大于10时使用缓存
* 当id小于或等于10时不使用缓存
* @param id
* @return
*/
@Cacheable(value = "id",key = "#id",condition = "#id>10")
public boolean demo(Integer id){
System.out.println("使用了数据库");
if(id==20){
return true;
}
return false;
}
}
6.测试
http://127.0.0.1:1111/demo?id=20