tio-boot框架整合ehcache實(shí)現(xiàn)過(guò)程示例
tio-boot整合ehcache
Tio-boot 是一個(gè)基于Java的網(wǎng)絡(luò)編程框架,用于快速開(kāi)發(fā)高性能的網(wǎng)絡(luò)應(yīng)用程序。
Ehcache 是一個(gè)廣泛使用的開(kāi)源Java緩存,它可以提高應(yīng)用程序的性能和擴(kuò)展性。
整合ecache需要用到jfinal-plugins
添加依賴
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <graalvm.version>23.1.1</graalvm.version> <tio.boot.version>1.2.9</tio.boot.version> <lombok-version>1.18.30</lombok-version> <hotswap-classloader.version>1.2.1</hotswap-classloader.version> <final.name>web-hello</final.name> <main.class>com.litongjava.tio.web.hello.HelloApp</main.class> </properties> <dependencies> <dependency> <groupId>com.litongjava</groupId> <artifactId>tio-boot</artifactId> <version>${tio.boot.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok-version}</version> <optional>true</optional> <scope>provided</scope> </dependency> <dependency> <groupId>com.litongjava</groupId> <artifactId>hotswap-classloader</artifactId> <version>${hotswap-classloader.version}</version> </dependency> <dependency> <groupId>com.litongjava</groupId> <artifactId>jfinal-plugins</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>activerecord</artifactId> <version>5.1.2</version> </dependency> </dependencies>
依賴解釋
- tio-boot是框架核心,
- jfinal-plugins提供與Ehcache的集成
- activerecord jfinal-plugins依賴jfinal-plugins
jfinal-plugins依賴如下
cron4j:2.2.5
ehcache-core:2.6.11
jedis:3.6.3
fst:2.57
添加配置文件ehcache.xml
ehcache.xml
是 Ehcache 緩存的配置文件。EcachePlugin啟動(dòng)時(shí)會(huì)自動(dòng)加載這個(gè)配置,它定義了緩存的基本屬性和行為。以下是文件中每個(gè)部分的詳細(xì)解釋?zhuān)?/p>
<diskStore>
: 指定磁盤(pán)存儲(chǔ)的路徑,用于溢出或持久化緩存數(shù)據(jù)到磁盤(pán)。<defaultCache>
: 設(shè)置默認(rèn)緩存的屬性。這些屬性將應(yīng)用于未單獨(dú)配置的所有緩存。eternal
: 設(shè)置為false
表示緩存不是永久的,可以過(guò)期。maxElementsInMemory
: 內(nèi)存中可以存儲(chǔ)的最大元素?cái)?shù)量。overflowToDisk
: 當(dāng)內(nèi)存中的元素?cái)?shù)量超過(guò)最大值時(shí),是否溢出到磁盤(pán)。diskPersistent
: 是否在JVM重啟之間持久化到磁盤(pán)。timeToIdleSeconds
: 元素最后一次被訪問(wèn)后多久會(huì)變成空閑狀態(tài)。timeToLiveSeconds
: 元素從創(chuàng)建或最后一次更新后多久會(huì)過(guò)期。memoryStoreEvictionPolicy
: 當(dāng)內(nèi)存達(dá)到最大值時(shí),移除元素的策略(例如,LRU表示最近最少使用)。
ehcache.xml配置文件內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir/EhCache" /> <defaultCache eternal="false" maxElementsInMemory="10000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="1800" timeToLiveSeconds="259200" memoryStoreEvictionPolicy="LRU" /> </ehcache>
EhCachePluginConfig 配置類(lèi)
這個(gè)類(lèi)是一個(gè)配置類(lèi),用于初始化和配置 Ehcache 插件。它通過(guò) @Configuration
注解標(biāo)記為配置類(lèi)。類(lèi)中的方法 ehCachePlugin
通過(guò) @Initialization
注解標(biāo)記為初始化方法。在這個(gè)方法中,創(chuàng)建了一個(gè) EhCachePlugin
實(shí)例并啟動(dòng)它。啟動(dòng)插件意味著 Ehcache 將根據(jù) ehcache.xml
配置文件的設(shè)置進(jìn)行初始化。
package com.litongjava.tio.web.hello.config; import com.litongjava.jfinal.aop.annotation.Configuration; import com.litongjava.jfinal.aop.annotation.Initialization; import com.litongjava.jfinal.plugin.ehcache.EhCachePlugin; @Configuration public class EhCachePluginConfig { @Initialization public void ehCachePlugin() { EhCachePlugin ehCachePlugin = new EhCachePlugin(); ehCachePlugin.start(); } }
控制器
EhCacheTestController:
- 這個(gè)控制器包含一個(gè)方法
test01
,用于測(cè)試將數(shù)據(jù)添加到 EhCache 緩存中并從中檢索數(shù)據(jù)。 - 在這個(gè)方法中,首先嘗試從緩存中獲取一個(gè)鍵值。如果不存在,它將計(jì)算一個(gè)新值并將其存儲(chǔ)在緩存中。
- 這個(gè)控制器演示了如何使用 Ehcache 存儲(chǔ)和檢索簡(jiǎn)單的鍵值對(duì)。
- 這個(gè)控制器包含一個(gè)方法
EhCacheController:
- 這個(gè)控制器包含多個(gè)方法,用于與 Ehcache 進(jìn)行更復(fù)雜的交互。
- 方法如
getCacheNames
和getAllCacheValue
用于檢索緩存中的信息,例如緩存名稱(chēng)或所有緩存的值。 - 其他方法允許按名稱(chēng)檢索特定緩存的值,或者根據(jù)緩存名稱(chēng)和鍵檢索特定的值。
- 這個(gè)控制器提供了更深入的視圖,展示了如何管理和檢查 Ehcache 中的數(shù)據(jù)。
package com.litongjava.tio.web.hello.controller; import com.litongjava.jfinal.plugin.ehcache.CacheKit; import com.litongjava.tio.http.server.annotation.RequestPath; import lombok.extern.slf4j.Slf4j; @Slf4j @RequestPath("/ecache/test") public class EhCacheTestController { public String test01() { String cacheName = "student"; String cacheKey = "litong"; String cacheData = CacheKit.get(cacheName, cacheKey); if (cacheData == null) { String result = "001"; log.info("計(jì)算新的值"); CacheKit.put(cacheName, cacheKey, result); } return cacheData; } }
訪問(wèn)測(cè)試 http://localhost/ecache/test/test01
package com.litongjava.tio.web.hello.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import com.litongjava.jfinal.plugin.ehcache.CacheKit; import com.litongjava.tio.http.server.annotation.RequestPath; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; @RequestPath("/ecache") public class EhCacheController { public String[] getCacheNames() { String[] cacheNames = CacheKit.getCacheManager().getCacheNames(); return cacheNames; } public Map<String, Map<String, Object>> getAllCacheValue() { CacheManager cacheManager = CacheKit.getCacheManager(); String[] cacheNames = cacheManager.getCacheNames(); Map<String, Map<String, Object>> retval = new HashMap<>(cacheNames.length); for (String name : cacheNames) { Map<String, Object> map = cacheToMap(cacheManager, name); retval.put(name, map); } return retval; } public Map<String, Object> getCacheValueByCacheName(String cacheName) { CacheManager cacheManager = CacheKit.getCacheManager(); Map<String, Object> retval = cacheToMap(cacheManager, cacheName); return retval; } public Object getCacheValueByCacheNameAndCacheKey(String cacheName, String key) { Object object = CacheKit.get(cacheName, key); return object; } private Map<String, Object> cacheToMap(CacheManager cacheManager, String name) { Cache cache = cacheManager.getCache(name); @SuppressWarnings("unchecked") List<String> keys = cache.getKeys(); Map<String, Object> map = new HashMap<>(keys.size()); for (String key : keys) { Element element = cache.get(key); Object value = element.getObjectValue(); map.put(key, value); } return map; } }
訪問(wèn)測(cè)試
http://localhost/ecache/getCacheNames
http://localhost/ecache/getAllCacheValue
以上就是tio-boot整合ehcache的詳細(xì)內(nèi)容,更多關(guān)于tio-boot整合ehcache的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java中對(duì)象的深復(fù)制(深克隆)和淺復(fù)制(淺克?。┙榻B
這篇文章主要介紹了Java中對(duì)象的深復(fù)制(深克?。┖蜏\復(fù)制(淺克?。?,需要的朋友可以參考下2015-03-03Java使用hutool工具實(shí)現(xiàn)驗(yàn)證碼登錄
這篇文章主要為大家詳細(xì)介紹了Java如何使用hutool工具實(shí)現(xiàn)驗(yàn)證碼登錄功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Spring Security 表單登錄功能的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Security 表單登錄,本文將構(gòu)建在之前簡(jiǎn)單的 Spring MVC示例 之上,因?yàn)檫@是設(shè)置Web應(yīng)用程序和登錄機(jī)制的必不可少的。需要的朋友可以參考下2019-06-06Java實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼(下載過(guò)程中可以暫停)
線程可以理解為下載的通道,一個(gè)線程就是一個(gè)文件的下載通道,多線程也就是同時(shí)開(kāi)啟好幾個(gè)下載通道,Java實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼(下載過(guò)程中可以暫停),有興趣的可以了解一下。2016-12-12MyBatis中一對(duì)多的xml配置方式(嵌套查詢/嵌套結(jié)果)
這篇文章主要介紹了MyBatis中一對(duì)多的xml配置方式(嵌套查詢/嵌套結(jié)果),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java springboot 整合 Nacos的實(shí)例代碼
這篇文章主要介紹了Java springboot 整合 Nacos的實(shí)例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04java.lang.IllegalStateException異常原因和解決辦法
這篇文章主要給大家介紹了關(guān)于java.lang.IllegalStateException異常原因和解決辦法,IllegalStateException是Java標(biāo)準(zhǔn)庫(kù)中的一個(gè)異常類(lèi),通常表示在不合適或無(wú)效的情況下執(zhí)行了某個(gè)方法或操作,需要的朋友可以參考下2023-07-07