Service
这个层我们要做缓存还有一些其他的处理比如事物,还有一些其他的业务逻辑,验证
项目结构
缓存
缓存注解的使用
@Cacheable
Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件
@CachePut
和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法
@CacheEvict
方法执行成功后会从缓存中移除相应数据。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据(设置为true时会移除所有缓存)
pom
4.0.0
com.project
service
0.0.1-SNAPSHOT
jar
service
Demo project for Spring Boot
com.project
parent
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
com.project
dao
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-cache
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-devtools
runtime
org.projectlombok
lombok
true
org.springframework.session
spring-session-data-redis
org.springframework.boot
spring-boot-starter-test
test
Java例子
package com.frog.mvcdemo.controller;
import com.frog.mvcdemo.entity.Frog;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@CacheConfig(cacheNames = "frogtest")
@RestController
@RequestMapping(value = "/frogtest")
public class FrogTestController {
@Cacheable()
@ApiOperation(value = "获取Frog的列表")
@RequestMapping(value = "",method = RequestMethod.GET)
public List show(){
List list = new ArrayList<>();
list.add(new Frog(1,"one",2,new Date(),"controllertest"));
list.add(new Frog(2,"two",3,new Date(),"controllertest"));
return list;
}
@Cacheable()
@ApiOperation(value = "获取Frog详细信息",notes = "根据id查询对应Frog信息")
@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int", paramType = "path")
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Map showById(@PathVariable int id){
Map map = new HashMap<>();
if(id == 1){
map.put("FROG",new Frog(1,"one",3,new Date(),"showById"));
map.put("RESULT","SUCCESS");
}else{
map.put("FROG",null);
map.put("RESULT","ERROR");
}
return map;
}
@CacheEvict(allEntries = true)
@ApiOperation(value = "添加Frog详细信息",notes = "添加一条Frog的详细信息")
@ApiImplicitParam(name = "frog", value = "Frog实体", required = true, dataType = "Frog")
@RequestMapping(value = "",method = RequestMethod.POST)
public Map add(@RequestBody Frog frog){
Map map = new HashMap<>();
map.put("FROG",frog);
map.put("RESULT","SUCCESS");
return map;
}
@CacheEvict(allEntries = true)
@ApiOperation(value = "删除Frog详细信息",notes = "根据id删除对应Frog的详细信息")
@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int", paramType = "path")
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
public Map deleteById(@PathVariable int id){
Map map = new HashMap<>();
if(id != 0){
map.put("FROG",new Frog(id,"testfrog",id,new Date(),"deleteById"));
map.put("RESULT","SUCCESS");
}else{
map.put("FROG",null);
map.put("RESULT","ERROR");
}
return map;
}
@CacheEvict(allEntries = true)
@ApiOperation(value = "更新Frog详细信息",notes = "根据id更新Frog的详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int",paramType = "path"),
@ApiImplicitParam(name = "frog", value = "Frog实体", required = true, dataType = "Frog")
})
@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
public Map updateById(@PathVariable int id,@RequestBody Frog frog){
Map map = new HashMap<>();
map.put("FROG",frog);
map.put("RESULT","SUCCESS");
map.put("ID",id);
return map;
}
}
缓存一般加在dao层或service层,发生回滚时保持redis中数据不被清空,这里测试在Controller层加缓存。
@CacheConfig(cacheNames = “frogtest”)代表这个Controller下需要缓存的方法对应redis中的缓存名为frogtest~keys。
查询方法都用@Cacheable注解加入缓存
添加更新删除@CacheEvict(allEntries = true)注解表示执行方法时删除redis中缓存名frogtest~keys下所有缓存