tio-boot框架整合ehcache實(shí)現(xiàn)過程示例
tio-boot整合ehcache
Tio-boot 是一個基于Java的網(wǎng)絡(luò)編程框架,用于快速開發(fā)高性能的網(wǎng)絡(luò)應(yīng)用程序。
Ehcache 是一個廣泛使用的開源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啟動時會自動加載這個配置,它定義了緩存的基本屬性和行為。以下是文件中每個部分的詳細(xì)解釋:
<diskStore>: 指定磁盤存儲的路徑,用于溢出或持久化緩存數(shù)據(jù)到磁盤。<defaultCache>: 設(shè)置默認(rèn)緩存的屬性。這些屬性將應(yīng)用于未單獨(dú)配置的所有緩存。eternal: 設(shè)置為false表示緩存不是永久的,可以過期。maxElementsInMemory: 內(nèi)存中可以存儲的最大元素數(shù)量。overflowToDisk: 當(dāng)內(nèi)存中的元素數(shù)量超過最大值時,是否溢出到磁盤。diskPersistent: 是否在JVM重啟之間持久化到磁盤。timeToIdleSeconds: 元素最后一次被訪問后多久會變成空閑狀態(tài)。timeToLiveSeconds: 元素從創(chuàng)建或最后一次更新后多久會過期。memoryStoreEvictionPolicy: 當(dāng)內(nèi)存達(dá)到最大值時,移除元素的策略(例如,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 配置類
這個類是一個配置類,用于初始化和配置 Ehcache 插件。它通過 @Configuration 注解標(biāo)記為配置類。類中的方法 ehCachePlugin 通過 @Initialization 注解標(biāo)記為初始化方法。在這個方法中,創(chuàng)建了一個 EhCachePlugin 實(shí)例并啟動它。啟動插件意味著 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:
- 這個控制器包含一個方法
test01,用于測試將數(shù)據(jù)添加到 EhCache 緩存中并從中檢索數(shù)據(jù)。 - 在這個方法中,首先嘗試從緩存中獲取一個鍵值。如果不存在,它將計算一個新值并將其存儲在緩存中。
- 這個控制器演示了如何使用 Ehcache 存儲和檢索簡單的鍵值對。
- 這個控制器包含一個方法
EhCacheController:
- 這個控制器包含多個方法,用于與 Ehcache 進(jìn)行更復(fù)雜的交互。
- 方法如
getCacheNames和getAllCacheValue用于檢索緩存中的信息,例如緩存名稱或所有緩存的值。 - 其他方法允許按名稱檢索特定緩存的值,或者根據(jù)緩存名稱和鍵檢索特定的值。
- 這個控制器提供了更深入的視圖,展示了如何管理和檢查 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("計算新的值");
CacheKit.put(cacheName, cacheKey, result);
}
return cacheData;
}
}訪問測試 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;
}
}訪問測試
http://localhost/ecache/getCacheNames
http://localhost/ecache/getAllCacheValue
以上就是tio-boot整合ehcache的詳細(xì)內(nèi)容,更多關(guān)于tio-boot整合ehcache的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java Redis Template批量查詢指定鍵值對的實(shí)現(xiàn)
本文主要介紹了Java Redis Template批量查詢指定鍵值對的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
java中@EnableAutoConfiguration注解使用
在Spring Boot框架中,@EnableAutoConfiguration是一種非常重要的注解,本文就來介紹一下java中@EnableAutoConfiguration注解使用,感興趣的可以了解一下2023-11-11
Java?Web?Axios實(shí)現(xiàn)前后端數(shù)據(jù)異步交互實(shí)例代碼
Axios作為一個流行的前端?HTTP?通信庫,可以極大地簡化前端與后端之間的數(shù)據(jù)交互,這篇文章主要介紹了Java?Web?Axios實(shí)現(xiàn)前后端數(shù)據(jù)異步交互的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09

