java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > tio-boot整合ehcache

tio-boot框架整合ehcache实现过程示例

作者:李通

这篇文章主要为大家介绍了tio-boot框架整合ehcache实现过程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

tio-boot整合ehcache

Tio-boot 是一个基于Java的网络编程框架,用于快速开发高性能的网络应用程序。

Ehcache 是一个广泛使用的开源Java缓存,它可以提高应用程序的性能和扩展性。

整合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>

依赖解释

jfinal-plugins依赖如下

cron4j:2.2.5
ehcache-core:2.6.11
jedis:3.6.3
fst:2.57

添加配置文件ehcache.xml

ehcache.xml 是 Ehcache 缓存的配置文件。EcachePlugin启动时会自动加载这个配置,它定义了缓存的基本属性和行为。以下是文件中每个部分的详细解释:

ehcache.xml配置文件内容如下

<?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 注解标记为配置类。类中的方法 ehCachePlugin 通过 @Initialization 注解标记为初始化方法。在这个方法中,创建了一个 EhCachePlugin 实例并启动它。启动插件意味着 Ehcache 将根据 ehcache.xml 配置文件的设置进行初始化。

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();
  }
}

控制器

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的详细内容,更多关于tio-boot整合ehcache的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文