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