SpringBoot 整合 caffeine

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 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )