【问题标题】:EhCache Empty CacheEhCache 空缓存
【发布时间】:2013-09-27 15:38:36
【问题描述】:

我不确定我是否误解了ehcache 或者我是否没有正确实现它,但是在将某些内容保存到缓存之后,当我去检索它时,我有一个空缓存!

基本上我正在尝试使用 ehcache 来代替 @Singleton 的使用。我需要在应用程序中的一个位置存储可以从应用程序中的多个位置访问和共享的内存数据。

我目前的代码如下:

@Stateless
@LocalBean
@Startup
public class DevicePoll {

...

@Schedule(minute = "*/2", hour = "*")
    protected void getStatus() {
        // Get all the sites
        List<Site> sites = siteDAO.findAllSites();

        // Setup the cache manager
        CacheManager manager = CacheManager.getInstance();
        Cache cache = manager.getCache("DEVICE_STATUS_CACHE");

        // For testing lets get an item that we know was placed
        Element e = cache.get("201");

        for (Site site : sites) {
            // Obtain the devices
            List<Device> devices = deviceUtil.getDeviceTree(site);

            // Create a new element and place it in the cache
            Element element = new Element(site.getId(), devices);
            cache.put(element);

        }
        // Shutdown the cache manager
        manager.shutdown();
    }

...
}

我的 ehcache.xml 是:

<defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>

<cache name="DEVICE_STATUS_CACHE" maxEntriesLocalHeap="1000" eternal="true" memoryStoreEvictionPolicy="FIFO"/>

【问题讨论】:

  • 这是基于 Spring 框架的应用程序吗?
  • 不,我没有使用 Spring。

标签: ehcache


【解决方案1】:

CacheManager 是高地人,你必须只有一个。当你在你的方法中创建和销毁它时,什么都没有保存,你需要更多类似的东西:

public class DevicePoll {

    // usually cache and cache manager are injected
    private Cache cache;

    public DevicePoll() {
        final CacheManager manager = CacheManager.getInstance();
        this.cache = manager.getCache("DEVICE_STATUS_CACHE");
    }

    @Schedule(minute = "*/2", hour = "*")
    protected void getStatus() {
        // Get all the sites
        List<Site> sites = siteDAO.findAllSites();

        // For testing lets get an item that we know was placed
        Element e = cache.get("201");

        for (Site site : sites) {
            // Obtain the devices
            List<Device> devices = deviceUtil.getDeviceTree(site);

            // Create a new element and place it in the cache
            Element element = new Element(site.getId(), devices);
            cache.put(element);

        }
    }

    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2012-12-12
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多