首页
看点啥
插画图片
首页 看点啥 spring-boot整合缓存的初步使用

spring-boot整合缓存的初步使用

2026-08-05 0

1.创建一个demo项目

spring-boot整合缓存的初步使用

4.0.0org.springframework.bootspring-boot-starter-parent2.1.5.RELEASE com.exampledemo0.0.1-SNAPSHOTdemoDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-maven-plugin

2.配置DemoApplication文件

@SpringBootApplication@EnableCachingpublic class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

3.编写DemoController

@RestControllerpublic class DemoController {@Autowiredprivate DemoService demoService;@GetMapping("demo")public boolean demo(@RequestParam("id") Integer id){return demoService.demo(id);}}

4.编写DemoService

package com.example.demo;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Servicepublic class DemoService {/** * 当id大于10时使用缓存 * 当id小于或等于10时不使用缓存 * @param id * @return */@Cacheable(value = "id",key = "#id",condition = "#id>10")public boolean demo(Integer id){System.out.println("---->未使用缓存");if(id==20){ return true;}else{return false;}}}

5.配置服务端口

server.port=1111

6.测试

http://127.0.0.1:1111/demo?id=10

7.spring-boot使用tools-redis实现分布式缓存

喜欢(0)

上一篇

Java 后端实现Mock功能返回JSON文件

Java 后端实现Mock功能返回JSON文件

下一篇

spring-boot使用tools-redis实现分布式锁

spring-boot使用tools-redis实现分布式锁
猜你喜欢