iBATIS缓存的错误使用
标王2.0中用到了ibatis内置的缓存机制,昨天在自测时发现缓存不起作用,其实以前在bp和bss中都有遇到过这种情况,但一直都没太在意。今天仔细查下原因,发现了问题。
下面是bp中cacheMode的配置
<cacheModel id="MEMORY-CACHE" type="MEMORY" readOnly="true" serialize="false"> <flushInterval hours="24" /> <property name="STRONG" value="STRONG" /></cacheModel>
private MemoryCacheLevel cacheLevel = MemoryCacheLevel.WEAK; private Map cache = Collections.synchronizedMap(new HashMap()); public void configure(Properties props) { String refType = props.getProperty("reference-type"); if (refType == null) { refType = props.getProperty("referenceType"); } if (refType != null) { cacheLevel = MemoryCacheLevel.getByReferenceType(refType); } } public void putObject(CacheModel cacheModel, Object key, Object value) { Object reference = null; if (cacheLevel.equals(MemoryCacheLevel.WEAK)) { reference = new WeakReference(value); } else if (cacheLevel.equals(MemoryCacheLevel.SOFT)) { reference = new SoftReference(value); } else if (cacheLevel.equals(MemoryCacheLevel.STRONG)) { reference = new StrongReference(value); } cache.put(key, reference); }