依赖包:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-cache</artifactId>
复制代码 常用注解
在SpringCache中提供了很多缓存操作的注解,常见的是以下的几个:
注解 | 说明 | @EnableCaching | 开启缓存注解功能,通常加在启动类上 | @Cacheable | 在方法执行前先查询缓存中是否有数据,如果有数据,则直接返回缓存数据;如果没有缓存数据,调用方法并将方法返回值放到缓存中 | @CachePut | 将方法的返回值放到缓存中 | @CacheEvict | 将一条或多条数据从缓存中删除 |
在spring boot项目中,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在启动类上使用@EnableCaching开启缓存支持即可。 例如,使用Redis作为缓存技术,只需要导入Spring data Redis的maven坐标即可。
实现思路
实现步骤: 1). 导入Spring Cache和Redis相关maven坐标 2). 在启动类上加入@EnableCaching注解,开启缓存注解功能 3). 在用户端接口SetmealController的 list 方法上加入@Cacheable注解 4). 在管理端接口SetmealController的 save、delete、update、startOrStop等方法上加入CacheEvict注解
1). 导入Spring Cache和Redis相关maven坐标
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-cache</artifactId>
- </dependency>
复制代码 2). 在启动类上加入@EnableCaching注解,开启缓存注解功能
- package com.sky;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- @SpringBootApplication
- @EnableTransactionManagement //开启注解方式的事务管理
- @Slf4j
- <font color="#ff0000"><b>@EnableCaching</b></font>
- public class SkyApplication {
- public static void main(String[] args) {
- SpringApplication.run(SkyApplication.class, args);
- log.info("server started");
- }
- }
复制代码 3). 在用户端接口SetmealController的 list 方法上加入@Cacheable注解
- /**
- * 条件查询
- *
- * @param categoryId
- * @return
- */
- @GetMapping("/list")
- @ApiOperation("根据分类id查询套餐")
- @Cacheable(cacheNames = "setmealCache",key = "#categoryId") //key: setmealCache::100
- public Result<List<Setmeal>> list(Long categoryId) {
- Setmeal setmeal = new Setmeal();
- setmeal.setCategoryId(categoryId);
- setmeal.setStatus(StatusConstant.ENABLE);
- List<Setmeal> list = setmealService.list(setmeal);
- return Result.success(list);
- }
复制代码 4). 在管理端接口SetmealController的 save、delete、update、startOrStop等方法上加入CacheEvict注解
- /**
- * 新增套餐
- *
- * @param setmealDTO
- * @return
- */
- @PostMapping
- @ApiOperation("新增套餐")
- @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
- public Result save(@RequestBody SetmealDTO setmealDTO) {
- setmealService.saveWithDish(setmealDTO);
- return Result.success();
- }
- /**
- * 批量删除套餐
- *
- * @param ids
- * @return
- */
- @DeleteMapping
- @ApiOperation("批量删除套餐")
- @CacheEvict(cacheNames = "setmealCache",allEntries = true)
- public Result delete(@RequestParam List<Long> ids) {
- setmealService.deleteBatch(ids);
- return Result.success();
- }
- /**
- * 修改套餐
- *
- * @param setmealDTO
- * @return
- */
- @PutMapping
- @ApiOperation("修改套餐")
- @CacheEvict(cacheNames = "setmealCache",allEntries = true)
- public Result update(@RequestBody SetmealDTO setmealDTO) {
- setmealService.update(setmealDTO);
- return Result.success();
- }
- /**
- * 套餐起售停售
- *
- * @param status
- * @param id
- * @return
- */
- @PostMapping("/status/{status}")
- @ApiOperation("套餐起售停售")
- @CacheEvict(cacheNames = "setmealCache",allEntries = true)
- public Result startOrStop(@PathVariable Integer status, Long id) {
- setmealService.startOrStop(status, id);
- return Result.success();
- }
复制代码
|