【问题标题】:Code in Persistent EHcache docs not working java持久性 EHcache 文档中的代码不起作用 java
【发布时间】:2020-06-07 23:52:40
【问题描述】:

我想在我的java 项目中使用EHcache。他们有持久的存储支持。我已阅读文档 https://www.ehcache.org/documentation/2.7/configuration/fast-restart.html 找到了这段代码

Configuration cacheManagerConfig = new Configuration()
    .diskStore(new DiskStoreConfiguration()
        .path("/tmp/file.txt"));
CacheConfiguration cacheConfig = new CacheConfiguration()
    .name("my-cache")
    .maxBytesLocalHeap(16, MemoryUnit.MEGABYTES)
    .maxBytesLocalOffHeap(256, MemoryUnit.MEGABYTES)
    .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));

cacheManagerConfig.addCache(cacheConfig);

CacheManager cacheManager = new CacheManager(cacheManagerConfig);
Ehcache myCache = cacheManager.getEhcache("my-cache");

我已经导入了依赖,但它显示了很多错误。 我遇到的错误

'Configuration' is abstract; cannot be instantiated

请提供一些简单的步骤来使用这个库。我阅读了文档,但代码不起作用。帮我解决一些问题。

【问题讨论】:

  • 我没有完整的上下文,但你能告诉我们更多关于你在哪里尝试这个配置的细节吗?它是一个 Spring Boot 项目吗?我建议首先开始设置 ehcache,然后尝试对其进行配置以支持持久存储。一些可以帮助您入门的博文:baeldung.com/spring-boot-ehcachebaeldung.com/ehcache
  • 你在使用 ehcache 3 吗? (您链接的文档适用于 2.7 版)
  • ehcache 3 版本..

标签: java caching ehcache


【解决方案1】:

找到答案。

    try(PersistentCacheManager persistentCacheManager =
                newCacheManagerBuilder()
                        .with(persistence("/tmp/myProjectCache"))
                        .withCache("test-cache",
                                newCacheConfigurationBuilder(
                                        String.class, String.class,
                                        newResourcePoolsBuilder()
                                                .heap(1, EntryUnit.ENTRIES)
                                                .offheap(1, MemoryUnit.MB)
                                                .disk(2, MemoryUnit.MB, true)
                                )
                        ).build(true)) {
        org.ehcache.Cache cache = persistentCacheManager.getCache("test-cache", String.class, String.class);

        cache.put("name1","steven");
        cache.put("name2","prince");

        System.out.println(cache.get("name1"));
        System.out.println(cache.get("name2"));
    }

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    相关资源
    最近更新 更多