[ http://issues.apache.org/jira/browse/IBATIS-204?page=comments#action_12332542 ]
Clinton Begin commented on IBATIS-204: -------------------------------------- Two questions: 1) Why not just log in CacheModel, rather than within each implementation? 2) Why not attach source files instead of embedding code in the body of the issue? ;-) Clinton > Cache layer does no logging of puts, gets, flushes, or removals. > ---------------------------------------------------------------- > > Key: IBATIS-204 > URL: http://issues.apache.org/jira/browse/IBATIS-204 > Project: iBatis for Java > Type: Improvement > Components: SQL Maps > Versions: 2.0.8, 2.0.9, 2.1.0, 2.0.9b, 2.1.5, 2.2.0, 2.2.5 > Environment: All > Reporter: michael campbell > Priority: Minor > > The cache layer has no logging. Following is the code change I made (all > cache mechanisms EXCEPT os-cache) to the cache code which suits my purposes. > I tried to stay within the spirit of the existing logging, mimicing the > output formats, etc. Released to the public domain. > =======================/* > * Copyright 2004 Clinton Begin > * > * Licensed under the Apache License, Version 2.0 (the "License"); > * you may not use this file except in compliance with the License. > * You may obtain a copy of the License at > * > * http://www.apache.org/licenses/LICENSE-2.0 > * > * Unless required by applicable law or agreed to in writing, software > * distributed under the License is distributed on an "AS IS" BASIS, > * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > * See the License for the specific language governing permissions and > * limitations under the License. > */ > package com.ibatis.sqlmap.engine.cache.fifo; > import java.util.Collections; > import java.util.HashMap; > import java.util.LinkedList; > import java.util.List; > import java.util.Map; > import java.util.Properties; > import com.ibatis.common.logging.Log; > import com.ibatis.common.logging.LogFactory; > import com.ibatis.sqlmap.engine.cache.CacheController; > import com.ibatis.sqlmap.engine.cache.CacheModel; > /** > * FIFO (first in, first out) cache controller implementation > */ > public class FifoCacheController implements CacheController { > private static Log _log = LogFactory.getLog(FifoCacheController.class); > private Map cache; > private int cacheSize; > private List keyList; > /** > * Default constructor > */ > public FifoCacheController() { > this.cacheSize = 100; > this.cache = Collections.synchronizedMap(new HashMap()); > this.keyList = Collections.synchronizedList(new LinkedList()); > } > /** > * Configures the cache > * > * @param props > * Optionally can contain properties > [reference-type=WEAK|SOFT|STRONG] > */ > public void configure(Properties props) { > String size = props.getProperty("cache-size"); > if (size == null) { > size = props.getProperty("size"); > } > if (size != null) { > cacheSize = Integer.parseInt(size); > } > } > /** > * Flushes the cache. > * > * @param cacheModel > * The cache model > */ > public void flush(CacheModel cacheModel) { > if (_log.isDebugEnabled()) { > _log.debug("{cache-FIFO} Flush"); > } > cache.clear(); > keyList.clear(); > } > /** > * Get an object out of the cache. > * > * @param cacheModel > * The cache model > * @param key > * The key of the object to be returned > * @return The cached object (or null) > */ > public Object getObject(CacheModel cacheModel, Object key) { > Object value = cache.get(key); > if (_log.isDebugEnabled()) { > _log.debug("{cache-FIFO} GetObject-[" + key + "]-[" + value + "]"); > } > return value; > } > /** > * Add an object to the cache > * > * @param cacheModel > * The cacheModel > * @param key > * The key of the object to be cached > * @param value > * The object to be cached > */ > public void putObject(CacheModel cacheModel, Object key, Object value) { > if (_log.isDebugEnabled()) { > _log.debug("{cache-FIFO} PutObject-[" + key + "]-[" + value + "]"); > } > cache.put(key, value); > keyList.add(key); > if (keyList.size() > cacheSize) { > try { > Object oldestKey = keyList.remove(0); > cache.remove(oldestKey); > } catch (IndexOutOfBoundsException e) { > // ignore > } > } > } > public Object removeObject(CacheModel cacheModel, Object key) { > keyList.remove(key); > Object value = cache.remove(key); > if (_log.isDebugEnabled()) { > _log.debug("{cache-FIFO} RemoveObject-[" + key + "]-[" + value + "]"); > } > return value; > } > } > ============================================ > /* > * Copyright 2004 Clinton Begin > * > * Licensed under the Apache License, Version 2.0 (the "License"); > * you may not use this file except in compliance with the License. > * You may obtain a copy of the License at > * > * http://www.apache.org/licenses/LICENSE-2.0 > * > * Unless required by applicable law or agreed to in writing, software > * distributed under the License is distributed on an "AS IS" BASIS, > * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > * See the License for the specific language governing permissions and > * limitations under the License. > */ > package com.ibatis.sqlmap.engine.cache.lru; > import java.util.Collections; > import java.util.HashMap; > import java.util.LinkedList; > import java.util.List; > import java.util.Map; > import java.util.Properties; > import com.ibatis.common.logging.Log; > import com.ibatis.common.logging.LogFactory; > import com.ibatis.sqlmap.engine.cache.CacheController; > import com.ibatis.sqlmap.engine.cache.CacheModel; > /** > * LRU (least recently used) cache controller implementation > */ > public class LruCacheController implements CacheController { > private static Log _log = LogFactory.getLog(LruCacheController.class); > private Map cache; > private int cacheSize; > private List keyList; > /** > * Default constructor > */ > public LruCacheController() { > this.cacheSize = 100; > this.cache = Collections.synchronizedMap(new HashMap()); > this.keyList = Collections.synchronizedList(new LinkedList()); > } > /** > * Configures the cache > * > * @param props > * Optionally can contain properties > [reference-type=WEAK|SOFT|STRONG] > */ > public void configure(Properties props) { > String size = props.getProperty("cache-size"); > if (size == null) { > size = props.getProperty("size"); > } > if (size != null) { > cacheSize = Integer.parseInt(size); > } > } > /** > * Flushes the cache. > * > * @param cacheModel > * The cache model > */ > public void flush(CacheModel cacheModel) { > if (_log.isDebugEnabled()) { > _log.debug("{cache-LRU} Flush"); > } > cache.clear(); > keyList.clear(); > } > /** > * Get an object out of the cache. > * > * @param cacheModel > * The cache model > * @param key > * The key of the object to be returned > * @return The cached object (or null) > */ > public Object getObject(CacheModel cacheModel, Object key) { > Object result = cache.get(key); > if (_log.isDebugEnabled()) { > _log.debug("{cache-LRU} GetObject-[" + key + "]-[" + result + "]"); > } > keyList.remove(key); > if (result != null) { > keyList.add(key); > } > return result; > } > /** > * Add an object to the cache > * > * @param cacheModel > * The cacheModel > * @param key > * The key of the object to be cached > * @param value > * The object to be cached > */ > public void putObject(CacheModel cacheModel, Object key, Object value) { > if (_log.isDebugEnabled()) { > _log.debug("{cache-LRU} PutObject-[" + key + "]-[" + value + "]"); > } > cache.put(key, value); > keyList.add(key); > if (keyList.size() > cacheSize) { > try { > Object oldestKey = keyList.remove(0); > cache.remove(oldestKey); > } catch (IndexOutOfBoundsException e) { > // ignore > } > } > } > public Object removeObject(CacheModel cacheModel, Object key) { > keyList.remove(key); > Object value = cache.remove(key); > if (_log.isDebugEnabled()) { > _log.debug("{cache-LRU} RemoveObject-[" + key + "]-[" + value + "]"); > } > return value; > } > } > =============================================== > /* > * Copyright 2004 Clinton Begin > * > * Licensed under the Apache License, Version 2.0 (the "License"); > * you may not use this file except in compliance with the License. > * You may obtain a copy of the License at > * > * http://www.apache.org/licenses/LICENSE-2.0 > * > * Unless required by applicable law or agreed to in writing, software > * distributed under the License is distributed on an "AS IS" BASIS, > * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > * See the License for the specific language governing permissions and > * limitations under the License. > */ > package com.ibatis.sqlmap.engine.cache.memory; > import com.ibatis.common.logging.Log; > import com.ibatis.common.logging.LogFactory; > import com.ibatis.sqlmap.engine.cache.CacheController; > import com.ibatis.sqlmap.engine.cache.CacheModel; > import java.lang.ref.SoftReference; > import java.lang.ref.WeakReference; > import java.util.Collections; > import java.util.HashMap; > import java.util.Map; > import java.util.Properties; > /** > * Memory-based implementation of CacheController > */ > public class MemoryCacheController implements CacheController { > private MemoryCacheLevel cacheLevel = MemoryCacheLevel.WEAK; > private Map cache = Collections.synchronizedMap(new HashMap()); > private static Log _log = LogFactory.getLog(MemoryCacheController.class); > /** > * Configures the cache > * > * @param props > * Optionally can contain properties > [reference-type=WEAK|SOFT|STRONG] > */ > 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); > } > } > /** > * Add an object to the cache > * > * @param cacheModel > * The cacheModel > * @param key > * The key of the object to be cached > * @param value > * The object to be cached > */ > 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); > } > > if (_log.isDebugEnabled()) { > _log.debug("{cache-memory} PutObject-[" + key + "]-[" + value + "]"); > } > > cache.put(key, reference); > } > /** > * Get an object out of the cache. > * > * @param cacheModel > * The cache model > * @param key > * The key of the object to be returned > * @return The cached object (or null) > */ > public Object getObject(CacheModel cacheModel, Object key) { > Object value = null; > Object ref = cache.get(key); > if (ref != null) { > if (ref instanceof StrongReference) { > value = ((StrongReference)ref).get(); > } else if (ref instanceof SoftReference) { > value = ((SoftReference)ref).get(); > } else if (ref instanceof WeakReference) { > value = ((WeakReference)ref).get(); > } > } > if (_log.isDebugEnabled()) { > _log.debug("{cache-memory} GetObject-[" + key + "]-[" + value + "]"); > } > return value; > } > public Object removeObject(CacheModel cacheModel, Object key) { > Object value = null; > Object ref = cache.remove(key); > if (ref != null) { > if (ref instanceof StrongReference) { > value = ((StrongReference)ref).get(); > } else if (ref instanceof SoftReference) { > value = ((SoftReference)ref).get(); > } else if (ref instanceof WeakReference) { > value = ((WeakReference)ref).get(); > } > } > > if (_log.isDebugEnabled()) { > _log.debug("{cache-memory} RemoveObject-[" + key + "]-[" + value + "]"); > } > return value; > } > /** > * Flushes the cache. > * > * @param cacheModel > * The cache model > */ > public void flush(CacheModel cacheModel) { > if (_log.isDebugEnabled()) { > _log.debug("{cache-memory} Flush"); > } > cache.clear(); > } > /** > * Class to implement a strong (permanent) reference. > */ > private static class StrongReference { > private Object object; > /** > * StrongReference constructor for an object > * > * @param object - > * the Object to store > */ > public StrongReference(Object object) { > this.object = object; > } > /** > * Getter to get the object stored in the StrongReference > * > * @return - the stored Object > */ > public Object get() { > return object; > } > } > } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira