欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

tio-boot框架整合ehcache實(shí)現(xiàn)過(guò)程示例

 更新時(shí)間:2023年12月27日 09:30:06   作者:李通  
這篇文章主要為大家介紹了tio-boot框架整合ehcache實(shí)現(xiàn)過(guò)程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

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ì)。
  • 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)文章

最新評(píng)論