001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.cache.lru;
017:
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Properties;
024:
025: import com.ibatis.sqlmap.engine.cache.CacheController;
026: import com.ibatis.sqlmap.engine.cache.CacheModel;
027:
028: /**
029: * LRU (least recently used) cache controller implementation
030: */
031: public class LruCacheController implements CacheController {
032:
033: private int cacheSize;
034: private Map cache;
035: private List keyList;
036:
037: /**
038: * Default constructor
039: */
040: public LruCacheController() {
041: this .cacheSize = 100;
042: this .cache = Collections.synchronizedMap(new HashMap());
043: this .keyList = Collections.synchronizedList(new LinkedList());
044: }
045:
046: /**
047: * Configures the cache
048: *
049: * @param props Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]
050: */
051: public void configure(Properties props) {
052: String size = props.getProperty("cache-size");
053: if (size == null) {
054: size = props.getProperty("size");
055: }
056: if (size != null) {
057: cacheSize = Integer.parseInt(size);
058: }
059: }
060:
061: /**
062: * Add an object to the cache
063: *
064: * @param cacheModel The cacheModel
065: * @param key The key of the object to be cached
066: * @param value The object to be cached
067: */
068: public void putObject(CacheModel cacheModel, Object key,
069: Object value) {
070: cache.put(key, value);
071: keyList.add(key);
072: if (keyList.size() > cacheSize) {
073: try {
074: Object oldestKey = keyList.remove(0);
075: cache.remove(oldestKey);
076: } catch (IndexOutOfBoundsException e) {
077: //ignore
078: }
079: }
080: }
081:
082: /**
083: * Get an object out of the cache.
084: *
085: * @param cacheModel The cache model
086: * @param key The key of the object to be returned
087: * @return The cached object (or null)
088: */
089: public Object getObject(CacheModel cacheModel, Object key) {
090: Object result = cache.get(key);
091: keyList.remove(key);
092: if (result != null) {
093: keyList.add(key);
094: }
095: return result;
096: }
097:
098: public Object removeObject(CacheModel cacheModel, Object key) {
099: keyList.remove(key);
100: return cache.remove(key);
101: }
102:
103: /**
104: * Flushes the cache.
105: *
106: * @param cacheModel The cache model
107: */
108: public void flush(CacheModel cacheModel) {
109: cache.clear();
110: keyList.clear();
111: }
112:
113: }
|