使用Ehcache将对象缓存到JVM的内存和磁盘中

时间:2015-05-14 00:50:54

标签: ehcache

我想使用缓存管理500mb数据,所以我做了一个测试。我想将200mb数据放入JVM的内存中,将其他300mb放入磁盘中。

所以我放了2mb数据的每个元素,我设置了CacheManager的MaxBytesLocalHeap 250mb和Cache的MaxBytesLocalHeap 200mb,但是当我完成我的情况时,我发现JVM中有更多的170mb数据,磁盘数据超过290mb。我把钥匙打印在磁盘上,我发现有250个按键,而不是150个按键。

这意味着:磁盘数据有250个密钥,磁盘数据中有500mb数据,这不是我想要的......

我不知道为什么?我看了代码,但我没有找到原因,这是我的测试代码:

private static final String CACHE_PATH = "java.io.tmpdir";
private static String MAX_MEMORY = "512m";
private static String BLOCK_MAX_MEMORY = "512m";
private static String DATA_MAX_MEMORY = "512m";

private static MCacheManager instance;
private Properties props = null;
private CacheManager cacheManager = null;
private Cache memoryCache;
private Cache dataCache;

public static MCacheManager getInstance() {
    if (instance == null) {
        synchronized (MCacheManager.class) {
            if (instance == null) {
                instance = new MCacheManager();
            }
        }
    }
    return instance;
}

private MCacheManager() {
    super();
    double maxMemory = (Runtime.getRuntime().maxMemory() *0.4) / (1024*1024);//m
    MAX_MEMORY = maxMemory+"";
}

public Cache memoryCache() {
    if (memoryCache == null) {
        synchronized (this) {
            if (memoryCache == null) {
                String maxMemory = getProperty("block.cache.max.memory", BLOCK_MAX_MEMORY);
                memoryCache = getCache("__memoryCache__",maxMemory,false);
            }
        }
    }
    return memoryCache;
}

public Cache dataCache() {
    if (dataCache == null) {
        synchronized (this) {
            String maxMemory = getProperty("data.cache.max.memory", DATA_MAX_MEMORY);
            dataCache = getCache("__dataCache__",maxMemory,true);
        }
    }
    return dataCache;
}

private Cache getCache(String cacheName, String maxMemory, boolean saveDisk) {
    Cache cache = getCacheManager().getCache(cacheName);
    if (cache == null) {
        synchronized (this) {
            cache = getCacheManager().getCache(cacheName);
            if (cache == null) {
                CacheConfiguration cacheConfig = new CacheConfiguration();
                cacheConfig.name(cacheName);
                cacheConfig.eternal(false);
                cacheConfig.setMemoryStoreEvictionPolicy("LRU");
                cacheConfig.setOverflowToDisk(saveDisk);
                cacheConfig.setDiskPersistent(true);
                cacheConfig.setMaxBytesLocalHeap(maxMemory);
                cache = new Cache(cacheConfig);
                getCacheManager().addCache(cache);
            }
        }
    }
    return cache;
}

private CacheManager getCacheManager() {
    if (cacheManager == null) {
        synchronized (this) {
            if (cacheManager == null) {
                Configuration configuration = new Configuration();
                DiskStoreConfiguration dscg = new DiskStoreConfiguration();
                dscg.setPath(getProperty("cache.path.disk", CACHE_PATH));
                configuration.diskStore(dscg);
                configuration.setMaxBytesLocalHeap(getProperty("cache.manager.max.memory", MAX_MEMORY));
                configuration.setDynamicConfig(true);
                configuration.setMonitoring("autodetect");
                configuration.setUpdateCheck(false);
                cacheManager = new CacheManager(configuration);
            }
        }
    }
    return cacheManager;
}

private String getProperty(String key, String defaultValue) {
    if (props == null) {
        synchronized (this) {
            if (props == null) {
                try {
                    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/cache.properties");
                    if (stream == null) {
                        stream = MCacheManager.class.getResourceAsStream("/cache.properties");
                    }
                    props = new Properties();
                    props.load(stream);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }
        }
    }
    return props.getProperty(key, defaultValue);
}

这是cache.properties:

  #######################您的缓存设置      ########缓存文件路径############## = 100m cache.path.disk = f:\      ######## cacheManager max memorySize k | K | m | M | g | G ############## = 100m cache.manager.max.memory = 1000m      ######## BlockSet缓存      ######## max memory,k | K | m | M | g | G ############## = 100m block.cache.max.memory = 100m      ######## DataSet Cache      ######## max memory,k | K | m | M | g | G ############## = 100m data.cache.max.memory = 100m

我想要一些希望:300mb数据。 如何使用缓存来管理JVM中的200mb数据和300mb数据到磁盘......

0 个答案:

没有答案