我们可以使用 Redis可视化工具Redis Desktop Manager
来实时查看redis
的使用情况
@Slf4j
@RestController
@RequestMapping("api")
public class DemoController {
@GetMapping("info")
@PreAuth("hasRole('administrator')")
@Cacheable(cacheNames = "demo-info", key = "#name")
public R<String> info(String name) {
log.info("本条信息没有从缓存获取");
return R.data("Hello, My Name Is: " + name);
}
}
Redis Desktop Manager
,是没有缓存的键值的Redis Desktop Manager
,发现多了一个键值,正是我们设置的@Slf4j
@RestController
@RequestMapping("api")
public class DemoController {
@GetMapping("info")
@PreAuth("hasRole('administrator')")
@Cacheable(cacheNames = "demo-info", key = "#name")
public R<String> info(String name) {
log.info("本条信息没有从缓存获取");
return R.data("Hello, My Name Is: " + name);
}
@GetMapping("remove-info")
@PreAuth("hasRole('administrator')")
@CacheEvict(cacheNames = "demo-info", key = "#name")
public R<String> removeInfo(String name) {
return R.success("删除缓存成功");
}
}
删除缓存成功
Redis Desktop Manager
,发现缓存被移除info
接口,发现又如一开始,控制台打印了日志,并且Redis增加了键值好了,简单的缓存概念及其使用已经讲解完毕,更深一层的,我们可以查阅Spring的官方文档进行深入学习。
官方文档直达:https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache