spring-boot整合缓存的初步使用
1.创建一个demo项目

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>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=11116.测试
http://127.0.0.1:1111/demo?id=107.spring-boot使用tools-redis实现分布式缓存
-
08.05
《无极仙途》前期灵石获取做法分享
-
08.05
无极仙途洪荒仙藏如何玩 洪荒仙藏玩法介绍
-
08.05
《无极仙途》金秋活动预告
-
08.05
《无极仙途》洪荒仙藏活动说明
-
08.05
《无极仙途》龙伯钓鳌通关攻略
-
08.05
《无极仙途》未成年人防沉迷调整 防沉迷系统一览
推荐专题
热门阅读
-
- 《幻兽帕鲁》快速获取毕业打工帕鲁做法
- 08.05
-
-
- AI 生活工具的通知设计:少发一条,可能更像懂你
- 08.05
-
- spring-boot整合缓存的初步使用
- 08.05
-
-
-
-
下载
- |
-
-
下载
- 《行尸走肉第一章》免安装中文汉化硬盘版下载
- 单机|436 MB
- 一款以动作冒险为主题的游戏
-
-
下载
- 《街头霸王X铁拳》免安装中文汉化硬盘版下载
- 单机|111MB
- 一款非常好玩的格斗游戏
-
-
下载
- |
-
-
下载
- 《暗黑破坏神3》免安装繁体中文正式版下载
- 单机|7630 MB
- 一款以角色扮演为主题的游戏
-
-
下载
- 《马克思佩恩3》免安装硬盘版下载
- 单机|27033 MB
- 一款以第三人称射击为主题的游戏