Using Ehcache to cache objects into JVM's memory and into Disk -
i want use cache manage 500mb data ,so made test. want put 200mb data jvm's memory , other 300mb put disk.
so every element had put 2mb data, had set cachemanager's maxbyteslocalheap 250mb , cache's maxbyteslocalheap 200mb, when finished case, found there more 170mb data in jvm , disk data more 290mb. printed keys in disk, found there 250 keys, not 150 keys.
it means that: disk data has 250 keys , there 500mb data in disk data , it's not wanted ...
i don't know why ? watched codes, hadn't find reasons , test code :
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); }
this cache.properties :
####################### cache settings ######## cache file path ############## = 100m cache.path.disk = f:\ ######## cachemanager max memorysize k|k|m|m|g|g############## = 100m cache.manager.max.memory = 1000m ######## blockset cache ######## 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
i want hope : 300mb data . how use cache manage 200mb data in jvm , 300mb data disk ...
Comments
Post a Comment