1. 程式人生 > >springboot使用快取cache

springboot使用快取cache

springboot使用快取cache


1.引入jar包

		<!-- Springboot中開啟快取 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

2.在啟動類中開啟快取

@SpringBootApplication
@EnableAutoConfiguration
@EnableCaching
@MapperScan("com.learning.www.mapper")
public class LearningApplication {

	public static void main(String[] args) {
		SpringApplication.run(LearningApplication.class, args);
	}
}

3.在service的實現中加入快取(重點) 

	@Override
	@Cacheable(value = "ZphInfo", key = "#id")
	public ZphInfo getZphInfoById(int id) {
		return zphinfomapper.getZphInfoById(id);
	}

4.編寫controller

	@RequestMapping("getZphInfobyid")
	@ResponseBody
	public String getZphInfoById(String id) { 
		
		int zphid = Integer.parseInt(id);
		ZphInfo zphinfo = zphservice.getZphInfoById(zphid);
		
		return "根據查詢到了招聘會資訊:"+zphinfo.toString();
		
	}

5.驗證結果

第一次訪問:

————許可權認證————
2018-11-24 09:33:26.572 DEBUG 6292 --- [nio-8080-exec-3] c.l.w.m.U.getPasswordByUsername          : ==>  Preparing: select username,password,role from user where username=? 
2018-11-24 09:33:26.573 DEBUG 6292 --- [nio-8080-exec-3] c.l.w.m.U.getPasswordByUsername          : ==> Parameters: joke (String)
2018-11-24 09:33:26.576 DEBUG 6292 --- [nio-8080-exec-3] c.l.w.m.U.getPasswordByUsername          : <==      Total: 1
2018-11-24 09:33:40.554  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : URL : http://localhost:8080/zph/getZphInfobyid
2018-11-24 09:33:40.555  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : HTTP_METHOD : GET
2018-11-24 09:33:40.555  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : IP : 0:0:0:0:0:0:0:1
2018-11-24 09:33:40.557  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : CLASS_METHOD : com.learning.www.controller.ZphInfoController.getZphInfoById
2018-11-24 09:33:40.559  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : ARGS : [2]
2018-11-24 09:33:40.620 DEBUG 6292 --- [nio-8080-exec-5] c.l.w.m.ZphInfoMapper.getZphInfoById     : ==>  Preparing: select * from zphinfo where id = ? 
2018-11-24 09:33:40.621 DEBUG 6292 --- [nio-8080-exec-5] c.l.w.m.ZphInfoMapper.getZphInfoById     : ==> Parameters: 2(Integer)
2018-11-24 09:33:40.658 DEBUG 6292 --- [nio-8080-exec-5] c.l.w.m.ZphInfoMapper.getZphInfoById     : <==      Total: 1
2018-11-24 09:33:40.658  INFO 6292 --- [nio-8080-exec-5] c.l.www.controller.LoginController       : RESPONSE : 根據查詢到了招聘會資訊:ZphInfo(id=2, title=測試2, time=2019, place=null, add=null)

第二次訪問:沒有sql語句列印,說明未訪問資料庫,而是走了快取。

2018-11-24 09:35:06.512  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : URL : http://localhost:8080/zph/getZphInfobyid
2018-11-24 09:35:06.513  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : HTTP_METHOD : GET
2018-11-24 09:35:06.514  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : IP : 0:0:0:0:0:0:0:1
2018-11-24 09:35:06.515  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : CLASS_METHOD : com.learning.www.controller.ZphInfoController.getZphInfoById
2018-11-24 09:35:06.515  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : ARGS : [2]
2018-11-24 09:35:06.520  INFO 6292 --- [nio-8080-exec-8] c.l.www.controller.LoginController       : RESPONSE : 根據查詢到了招聘會資訊:ZphInfo(id=2, title=測試2, time=2019, place=null, add=null)

6.切換快取(未實測)

切換為EhCache作為快取

pom中新增一下依賴:

<dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

在resource 資料夾下新建ehcache的配置檔案ehcache.xml 內容如下,此檔案spring boot 會自動掃描 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!--切換為ehcache 快取時使用-->
<cache name="people" maxElementsInMemory="1000" />
</ehcache>

切換為Guava作為快取

只需要在pom中新增依賴

     <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>

問題:

不知道如何將List存入快取中,有大神知道的話,麻煩您給個連結或者下方評論指導一下,感激不盡!!