ehcache使用

在这里插入图片描述

依赖

 <dependencies>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
    </dependencies>

配置文件详解

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <!--diskStore:缓存数据持久化的目录 地址  -->
    <diskStore path="F:\develop\ehcache" />
    <defaultCache
        maxElementsInMemory="1000" 
        maxElementsOnDisk="10000000"
        eternal="false" 
        overflowToDisk="false" 
        diskPersistent="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120" 
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    </defaultCache>
</ehcache>
  • maxElementsInMemory: 内存中缓存的element的最大数目
  • maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制
  • eternal: 设定缓存的elements是否过期。如果为true,则缓存的数据始终有效,如果是false,要根据timeToIdleSeconds,timeToLiveSeconds判断
  • overflowToDisk:内存中数据超过限制,是否需要缓存到磁盘上
  • diskPersistent:是否在磁盘上持久化,重启jvm后,数据是否有效
  • timeToIdleSeconds:对象空闲时间,只对eternal属性为false起作用
  • timeToLiveSeconds:对象存活时间,只对eternal属性为false起作用 ,磁盘缓存的清理线程运行间隔
  • memoryStoreEvictionPolicy:超过内存空间,向磁盘存储数据的策略,可选:LRU 最近最少使用 FIFO 先进先出 LFU 最少使用

API代码(不使用xml)

public class ProgrammaticEhcacheDemo {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        DiskStoreConfiguration diskStoreConfigurationParameter = new DiskStoreConfiguration();
        diskStoreConfigurationParameter.setPath("/opt/test");
        configuration.addDiskStore(diskStoreConfigurationParameter);
        CacheManager cacheManager = CacheManager.create(configuration);

        CacheConfiguration cacheConfiguration = new CacheConfiguration("test",10);
        PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
        persistenceConfiguration.strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP);
        cacheConfiguration.persistence(persistenceConfiguration);
        Cache cache = new Cache(cacheConfiguration);
        cacheManager.addCache(cache);


        Cache test = cacheManager.getCache("test");

        test.put(new Element(1L,"Hello world"));

        System.out.println(test.get(1L));
        cacheManager.shutdown();
    }
}

使用XML

public class XmlEhcacheDemo {
    public static void main(String[] args) throws URISyntaxException {
        URL resource = XmlEhcacheDemo.class.getClassLoader().getResource("ehcache.xml");
        CacheManager cacheManager = CacheManager.create(resource);

        CacheConfiguration cacheConfiguration = new CacheConfiguration("test",10);
        PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
        persistenceConfiguration.strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP);
        cacheConfiguration.persistence(persistenceConfiguration);
        Cache cache = new Cache(cacheConfiguration);
        cacheManager.addCache(cache);


        Cache test = cacheManager.getCache("sampleCache");

        test.put(new Element(10L,"Hello world"));

        System.out.println(test.get(10L));
    }
}

与Spring的结合

public class SpringEhcacheDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        context.register(SpringEhcacheDemo.class);

        context.refresh();

        CacheManager cacheManager = context.getBean(CacheManager.class);
        Cache springCache = cacheManager.getCache("springCache");
        springCache.put(1L,"ttttt");
        Cache.ValueWrapper valueWrapper = springCache.get(1L);
        System.out.println(valueWrapper.get());

        EhCacheCacheManager ehCacheCacheManager =  (EhCacheCacheManager)cacheManager;
        ehCacheCacheManager.getCacheManager().shutdown();
        context.close();
    }

    @Bean
    public CacheManager ehCacheCacheManager(){
        URL resource = XmlEhcacheDemo.class.getClassLoader().getResource("ehcache-spring.xml");
        EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
        net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.create(resource);
        ehCacheCacheManager.setCacheManager(cacheManager);
        return ehCacheCacheManager;
    }
}

github代码地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您提供关于 Ehcache 的详细教程。以下是使用 Ehcache 的一般步骤: 1. 引入 Ehcache 依赖:在您的项目中,您需要添加 Ehcache 的依赖。可以使用 Maven 或 Gradle 进行管理。以下是 Maven 的配置示例: ```xml <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.8.1</version> </dependency> ``` 2. 创建一个 Ehcache 配置文件:Ehcache 使用 XML 文件来配置缓存。您可以创建一个名为 `ehcache.xml` 的文件,并将其放置在类路径下。以下是一个简单的配置示例: ```xml <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd"> <cache alias="myCache"> <resources> <heap unit="entries">100</heap> <offheap unit="MB">1</offheap> </resources> </cache> </config> ``` 3. 初始化和获取缓存:在您的代码中,您需要初始化 Ehcache 缓存管理器,并获取所需的缓存实例。以下是示例代码: ```java import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.xml.XmlConfiguration; public class Main { public static void main(String[] args) { XmlConfiguration xmlConfig = new XmlConfiguration(Main.class.getResource("/ehcache.xml")); CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig); cacheManager.init(); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); myCache.put("key", "value"); String value = myCache.get("key"); System.out.println(value); cacheManager.close(); } } ``` 在上面的示例中,我们首先使用 `XmlConfiguration` 类将 XML 配置文件加载为 `CacheManagerBuilder`。然后,我们使用 `cacheManager` 获取名为 "myCache" 的缓存实例,并将键值对放入缓存。最后,我们从缓存中获取值并打印出来。 这是一个简单的 Ehcache 使用教程。您可以根据自己的需求进行更高级和复杂的配置。希望对您有所帮助!如有任何问题,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值