【視頻&交流平台】
à SpringBoot視頻:http://t.cn/R3QepWG
à SpringCloud視頻:http://t.cn/R3QeRZc
à Spring Boot源碼:https://gitee.com/happyangellxq520/spring-boot
à Spring Boot交流平台:http://412887952-qq-com.iteye.com/blog/2321532
說明
(1)Spring Boot 版本:2.0.3.RELEASE
(2)jetcache版本:2.5.2
(3)JDK:1.8
前言
在前面我們對jetcache有了簡單的了解,那么怎么在Spring Boot中進行使用,這才是關鍵。本節文章會簡單的介紹下如何在SpringBoot中使用jetcache。
一、基本配置
這里我們以redis進行講解,對於在SpringBoot中引入jetcache還是很簡單。
1.1 pom文件中添加依賴
<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis</artifactId>
<version>2.5.2</version>
</dependency>
以上添加了jetcache redis的依賴,這樣就能使用jetcache操作redis了。
1.2 添加配置
在application.properties添加如下配置:
###jetcache
jetcache.statIntervalMinutes=15
jetcache.areaInCacheName=false
jetcache.local.default.type=linkedhashmap
jetcache.local.default.keyConvertor=fastjson
jetcache.remote.default.type=redis
jetcache.remote.default.keyConvertor=fastjson
jetcache.remote.default.valueEncoder=java
jetcache.remote.default.valueDecoder=java
jetcache.remote.default.poolConfig.minIdle=5
jetcache.remote.default.poolConfig.maxIdle=20
jetcache.remote.default.poolConfig.maxTotal=50
jetcache.remote.default.host=127.0.0.1
jetcache.remote.default.port=6379
這里對jetcache進行了配置,我們不需要單獨在配置redis了。
1.3 激活Cached和CreateCache注解
在啟動類中添加兩個注解@EnableMethodCache和@EnableCreateCacheAnnotation,這兩個注解分別激活Cached和CreateCache注解:
@SpringBootApplication
@EnableMethodCache(basePackages = "com.kfit.jetcache")
@EnableCreateCacheAnnotation
public class Springboot2JetcacheApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2JetcacheApplication.class, args);
}
}
到這里配置的地方就ok了,接下來就是具體的使用環節了。
二、使用方式
2.1 創建緩存實例
通過@CreateCache注解創建一個緩存實例:
@CreateCache
private Cache<Long,ArticleType> cache;
用起來就像map一樣:
//ready for entity.
ArticleType articleType = new ArticleType();
articleType.setId(100L);
articleType.setName("小說");
//set to cache.
cache.put(articleType.getId(), articleType);
//get from cache
articleType = cache.get(articleType.getId());
//remove from cache
cache.remove(articleType.getId());
那么這個生成的key的name是:
c.k.j.d.c.TestCacheInstanceController.cache100
那么如何自定義key的名稱呢?這個下節在講jetcache小技巧的時候統一進行講解。
2.2 創建方法緩存
使用@Cached方法可以為一個方法添加上緩存。JetCache通過Spring AOP生成代理,來支持緩存功能。注解可以加在接口方法上也可以加在類方法上,但需要保證是個Spring bean:
@Cached
public ArticleType getById(long id) {
ArticleType articleType = new ArticleType();
articleType.setId(id);
articleType.setName("視頻-"+id);
return articleType;
}
這個默認生成的redis的name是:
c.k.j.d.s.ArticleTypeService.getById(J)[102]
本文只是介紹了最基本的使用姿勢,今天連走帶跑了20公里,有點迷糊了,明天接着寫,jetcache使用過程中的小技巧。
歷史相關文章:
微信公眾號「SpringBoot」最近更新:
210. Spring Boot WebFlux:概念篇 Java8新特性:Stream:實戰篇 為了更勇敢,你可以害怕@一禪小和尚 Java8新特性:Stream:基礎篇 Java8新特性:方法引用 209. SpringBoot quartz:sqlserver啟動只有 DECLARE CURSOR 才允許使用... 風口之上,我是那頭豬嘛? Java8新特性:Lambda表達式: 摸摸里面 Java8新特性:Lambda表達式:過關斬將:使用場景 Java8新特性:Lambda表達式:小試牛刀 下雨天,適合學「Spring Boot」 Java8新特性:接口的默認方法 208. Spring Boot Swagger2:排序 – 漂游記 207. Spring Boot Swagger2:極簡方式 我讀的書很多,但都沒有你好看【一禪錄】 206. Spring Boot 2.0 Swagger2:使用 205. Spring Boot 2.0 Swagger2:初識Swagger 當要離開的時候,我卻動情了 205. jetcache:你需要知道的小技巧 204. jetcache:在Spring Boot中怎么玩?
搜索「springboot」或者掃描以下二維碼即可關注: