1、pom 加入
1 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-cache</artifactId> 5 </dependency> 6 7 <!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine --> 8 <dependency> 9 <groupId>com.github.ben-manes.caffeine</groupId> 10 <artifactId>caffeine</artifactId> 11 <version>2.8.8</version> 12 </dependency>
2、配置
1# caffeine cache configuration 2spring.cache.cache-names=user 3spring.cache.caffeine.spec=initialCapacity=50,maximumSize=500,expireAfterWrite=30s 4spring.cache.type=caffeine
3、启用缓存
1@EnableCaching 2@Log4j2 3@SpringBootApplication 4public class TestApplication { 5 6 public static void main(String[] args) { 7 Main.run(TestApplication.class, args); 8 } 9}
4、测试代码-模型
1@Data 2@NoArgsConstructor 3@AllArgsConstructor 4public class User implements Serializable { 5 6 private Long id; 7 private String name; 8}
5、测试代码-服务类
1@Service 2@Log4j2 3public class UserService { 4 5 private static List<User> users = new ArrayList<>(); 6 7 static { 8 User user1 = new User(1L, "lolo"); 9 User user2 = new User(2L, "popo"); 10 users.add(user1); 11 users.add(user2); 12 } 13 14 @CachePut(value = "user", key = "#id") 15 public User saveOrUpdateUser(Long id, String name) { 16 log.error("saveOrUpdateUser id:{}", id); 17 log.error("saveOrUpdateUser users:{}", users.size()); 18 User findUser = getUser(id); 19 if (findUser == null) { 20 findUser = new User(id, name); 21 users.add(findUser); 22 } else { 23 findUser.setName(name); 24 } 25 return findUser; 26 } 27 28 @Cacheable(value = "user", key = "#id") 29 public User getUser(Long id) { 30 log.error("getUser:{}", id); 31 Predicate<User> idPredicate = value -> value.getId().equals(id); 32 33 Optional<User> optionalUser = users.stream().filter(idPredicate).findFirst(); 34 if (optionalUser.isPresent()) { 35 User findUser = optionalUser.get(); 36 return findUser; 37 } 38 39 return null; 40 } 41 42 @CacheEvict(value = "user", key = "#id") 43 public void delete(Long id) { 44 log.error("delete:{}", id); 45 User user = getUser(id); 46 users.remove(user); 47 } 48}
6、控制器代码
1@Log4j2 2@Api(value = "首页") 3@RestController 4public class IndexController { 5 6 private final RedisTemplate<String, String> redisTemplate; 7 private final UserService userService; 8 9 public IndexController(RedisTemplate<String, String> redisTemplate, UserService userService) { 10 this.redisTemplate = redisTemplate; 11 this.userService = userService; 12 } 13 14 @ApiOperation(value = "新增或修改用户") 15 @PostMapping("/saveOrUpdateUser") 16 public ResponseEntity<Result<User>> saveOrUpdateUser( 17 @ApiParam(value = "用户ID") @RequestParam Long id, 18 @ApiParam(value = "用户姓名") @RequestParam String name 19 ) { 20 User user = userService.saveOrUpdateUser(id, name); 21 return ResultUtils.ok(user, "保存成功"); 22 } 23 24 @ApiOperation(value = "查询用户") 25 @GetMapping("/getUserById") 26 public ResponseEntity<Result<User>> getUserById( 27 @ApiParam(value = "用户ID") @RequestParam Long id 28 ) { 29 User user = userService.getUser(id); 30 return ResultUtils.ok(user, "查询成功"); 31 } 32 33 @ApiOperation(value = "删除用户") 34 @PostMapping("/deleteById") 35 public ResponseEntity<Result<Long>> deleteById( 36 @ApiParam(value = "用户ID") @RequestParam Long id 37 ) { 38 userService.delete(id); 39 return ResultUtils.ok(id, "删除成功"); 40 }