JAVA配置使用ehcache作为缓存实现提高性能------JAVA

<ehcache>
<!-- 硬盘的缓存位置 -->
    <diskStore path="java.io.tmpdir/ehcache"/>
<!-- 默认路径 -->
   <defaultCache
           maxEntriesLocalHeap="10000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           maxEntriesLocalDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU"
   >
       <persistence strategy="localTempSwap"/>
   </defaultCache>
   <cache name="HelloWorldCache"
          maxElementsInMemory="1000"
          eternal="false"
          timeToIdleSeconds="5"
          timeToLiveSeconds="5"
          overflowToDisk="false"
          memoryStoreEvictionPolicy="LRU"
   />
</ehcache>
<ehcache>
<!-- 硬盘的缓存位置 -->
    <diskStore path="java.io.tmpdir/ehcache"/>
<!-- 默认路径 -->
   <defaultCache
           maxEntriesLocalHeap="10000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           maxEntriesLocalDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU"
   >
       <persistence strategy="localTempSwap"/>
   </defaultCache>
   <cache name="HelloWorldCache"
          maxElementsInMemory="1000"
          eternal="false"
          timeToIdleSeconds="5"
          timeToLiveSeconds="5"
          overflowToDisk="false"
          memoryStoreEvictionPolicy="LRU"
   />
</ehcache>
package com.alatus.ehcache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import java.io.InputStream;

public class TestEh {
    public static void main(String[] args) {
//        读取配置信息,获取流对象
        InputStream input = TestEh.class.getClassLoader().getResourceAsStream("ehcache.xml");
//        获取缓存管理对象
        CacheManager cacheManager = new CacheManager(input);
//        获取缓存对象
        Cache cache = cacheManager.getCache("HelloWorldCache");
//        创建缓存数据
        Element element = new Element("username","Jack");
//        存入缓存
        cache.put(element);
//        取出值
        Element cacheInfo = cache.get("username");
        System.out.println(cacheInfo);
        System.out.println(cacheInfo.getObjectValue());
    }
}
package com.alatus.ehcache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import java.io.InputStream;

public class TestEh {
    public static void main(String[] args) {
//        读取配置信息,获取流对象
        InputStream input = TestEh.class.getClassLoader().getResourceAsStream("ehcache.xml");
//        获取缓存管理对象
        CacheManager cacheManager = new CacheManager(input);
//        获取缓存对象
        Cache cache = cacheManager.getCache("HelloWorldCache");
//        创建缓存数据
        Element element = new Element("username","Jack");
//        存入缓存
        cache.put(element);
//        取出值
        Element cacheInfo = cache.get("username");
        System.out.println(cacheInfo);
        System.out.println(cacheInfo.getObjectValue());
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.alatus</groupId>
    <artifactId>ehcache</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.6.11</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.alatus</groupId>
    <artifactId>ehcache</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.6.11</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值