springboot+EHcache 實(shí)現(xiàn)文章瀏覽量的緩存和超時(shí)更新
問(wèn)題描述
當(dāng)我們需要統(tǒng)計(jì)文章的瀏覽量的時(shí)候,最常規(guī)的做法就是:
1.訪問(wèn)文章鏈接www.abc.com/article/{id}
2.在控制層獲取Article實(shí)體
3.得到文章瀏覽量count并且count++
4.最后update實(shí)體Article。
這么做對(duì)沒(méi)有訪問(wèn)量的網(wǎng)站來(lái)說(shuō)很棒,如果網(wǎng)站訪問(wèn)量很大,這么不停的讀寫數(shù)據(jù)庫(kù),會(huì)對(duì)服務(wù)器造成很大的壓力。
解決思路
引入Ehcache,將文章的訪問(wèn)量存在cache中,每點(diǎn)擊一次文章,將cache中的count加1.在有效的時(shí)間內(nèi)訪問(wèn)文章只是將cache中的數(shù)據(jù)+1,超過(guò)指定時(shí)間則進(jìn)行一次數(shù)據(jù)庫(kù)更新。
解決方案
本文是在springboot整合ehcache的環(huán)境下驗(yàn)證的。springboot版本1.5.2 。ehcache版本2.6.11。springboot整合ehcache的步驟很簡(jiǎn)單,下面簡(jiǎn)單提一下,在pom文件中引入ehcache依賴
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency>
在類路徑下存放ehcache.xml文件。
在application.yml中指定:
spring: cache: jcache: config: classpath:ehcache.xml
最后在啟動(dòng)類標(biāo)注@EnableCaching
引入緩存之后,接著我們的正題
在ehcache.xml文件中定義dayHits緩存
<cache name="dayHits" maxEntriesLocalHeap="500" eternal="true" overflowToDisk="true"> </cache>
表示保存當(dāng)日點(diǎn)擊量的
在controller層定義緩存點(diǎn)擊量的方法
public Integer cacheCount(Long articleId){
Content content = contentRepository.findOne(articleId);
Ehcache cache = cacheManager.getEhcache("dayHits");
Element element = cache.get(articleId+"_count");
Integer count = 0;
if(element!=null){
count = (Integer) element.getValue();
}else{
count = content.getHits()== null?0:content.getHits();
}
count++;
cache.put(new Element(articleId+"_count",count));
cache.put(new Element(articleId+"_dayHitsDate",SystemUtils.getNowDate()));
Long time = System.currentTimeMillis();
if(time > (viewArticleTime+ 300000)){
viewArticleTime = time;
content.setHits(count);
contentRepository.save(content);
cache.removeAll();
}
return count;
}
3.在查看文章方法中進(jìn)行調(diào)用。
@RequestMapping(value = "article/{id}",method = RequestMethod. GET)
public String detail(@PathVariable Long id,ModelMap map){
Integer hits = cacheCount(id);
}
4.其中局部變量的定義:
private static CacheManager cacheManager = CacheManager.newInstance(); private static Long viewArticleTime = System.currentTimeMillis();
5.保存訪問(wèn)看看效果吧。
以上所述是小編給大家介紹的springboot+EHcache 實(shí)現(xiàn)文章瀏覽量的緩存和超時(shí)更新,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SWT(JFace)體驗(yàn)之打開(kāi)多個(gè)Form
SWT(JFace)體驗(yàn)之打開(kāi)多個(gè)Form的實(shí)現(xiàn)代碼。2009-06-06
Springboot集成knife4j實(shí)現(xiàn)風(fēng)格化API文檔
這篇文章主要介紹了Springboot如何集成knife4j實(shí)現(xiàn)風(fēng)格化API文檔,幫助大家更好的使用springboot框架,感興趣的朋友可以了解下2020-12-12
Java 必知必會(huì)的 URL 和 URLConnection使用
這篇文章主要介紹了Java 必知必會(huì)的 URL 和 URLConnection使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java實(shí)現(xiàn)布隆過(guò)濾器的幾種方式總結(jié)
這篇文章給大家總結(jié)了幾種Java實(shí)現(xiàn)布隆過(guò)濾器的方式,手動(dòng)硬編碼實(shí)現(xiàn),引入Guava實(shí)現(xiàn),引入hutool實(shí)現(xiàn),通過(guò)redis實(shí)現(xiàn)等幾種方式,文中有詳細(xì)的代碼和圖解,需要的朋友可以參考下2023-07-07
SpringSecurity6.x多種登錄方式配置小結(jié)
SpringSecurity6.x變了很多寫法,本文就來(lái)介紹一下SpringSecurity6.x多種登錄方式配置小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Java多線程Future實(shí)現(xiàn)優(yōu)雅獲取線程的執(zhí)行結(jié)果
這篇文章主要為大家詳細(xì)介紹了Java如何利用Future實(shí)現(xiàn)優(yōu)雅獲取線程的執(zhí)行結(jié)果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-07-07
@Scheduled定時(shí)器原理及@RefreshScope相互影響
這篇文章主要為大家介紹了@Scheduled定時(shí)器原理及@RefreshScope相互影響詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

