掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
在平時做項目都要用到緩存,方便臨時存儲一些數(shù)據(jù),加快訪問速度。如果項目比較小,搭建redis服務(wù),后期在維護上比較麻煩。今天分享一個SpringBoot集成Ehcache實現(xiàn)緩存的教程,適合中小項目中使用。

成都創(chuàng)新互聯(lián)是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括網(wǎng)站制作、成都網(wǎng)站制作、電商網(wǎng)站制作開發(fā)、小程序開發(fā)、微信營銷、系統(tǒng)平臺開發(fā),與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!
org.springframework.boot
spring-boot-starter-cache
org.ehcache
ehcache
3.8.1
javax.cache
cache-api
1.1.1
@MapperScan("com.zhangls.ehcache.dao.**")
@SpringBootApplication
@EnableCaching
public class EhcacheApplication {
public static void main(String[] args) {
SpringApplication.run(EhcacheApplication.class, args);
}
}在resources下增加ehcache.xml文件,配置如下:
xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
java.lang.String
com.zhangls.ehcache.entity.User
1
2000
100
spring:
cache:
jcache:
config: classpath:ehcache.xml
1.Ehcache 會在一定的規(guī)則下會序列化后存儲到硬盤上,因此緩存對象必須支持序列化。
public class User implements Serializable{}2.Spring定義了緩存接口Cache和管理緩存控制器 CacheManager,路徑為:
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
@Autowired
private CacheManager cacheManager;
@GetMapping("/addCache")
public String addCache() {
User user = new User();
user.setUsername("九天銀河聊編程");
user.setAge(34);
Cache cache = cacheManager.getCache("UserCache");
cache.put("user", user);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
return sdf.format(now) + ": " + "保存成功";
}
@GetMapping("/getCache")
public String getCache() {
Cache cache = cacheManager.getCache("UserCache");
Cache.ValueWrapper res = cache.get("user");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
if (null != res) {
User user = (User) res.get();//這里獲取 ehcache.xml 中value-type 定義的類型,可以直接強轉(zhuǎn)。
return sdf.format(now) + ": " + "姓名:" + user.getUsername() + ",年齡:" + user.getAge();
}
return sdf.format(now) + ": " + "沒有找到緩存!";
}
執(zhí)行:127.0.0.1:8080/ehcache/addCache。
執(zhí)行:127.0.0.1:8080/ehcache/getCache。
1分鐘后執(zhí)行:127.0.0.1:8080/ehcache/getCache,緩存失效。
service代碼:
@Service
public class ImPersonServiceImpl implements ImPersonService{
@Resource
private PersonMapper personMapper;
@Override
@Cacheable(cacheNames = "PersonCache", key = "#personId")
public ImPerson selectByPrimaryKey(String personId){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
System.out.println(sdf.format(now) + ": 未命中緩存,請求數(shù)據(jù)庫");
return personMapper.selectByPrimaryKey(personId);
}
}
controller代碼:
@GetMapping("/getCachePerson")
public ImPerson getCachePerson() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date start = new Date();
System.out.println(sdf.format(start) + ":執(zhí)行開始------");
ImPerson person = imPersonService.selectByPrimaryKey("1");
Date end = new Date();
System.out.println(sdf.format(end) + ":執(zhí)行結(jié)束------");
return person;
}執(zhí)行兩次:127.0.0.1:8080/ehcache/getCachePerson。
控制臺只打印一次SQL信息,說明第二次請求從緩存中獲取。
@Cacheable(cacheNames = "PersonCache", condition = "#id > 1")
Spring 緩存注解是基于Spring AOP切面,必須走代理才能生效。同類調(diào)用或者子類調(diào)用父類帶有緩存注解的方法時屬于內(nèi)部調(diào)用,沒有走代理,所以注解不會生效。所以在使用@Cacheable時,一定要放在在service的實現(xiàn)類中進行調(diào)用。

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流