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.fifo;
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: * FIFO (first in, first out) cache controller implementation
030: */
031: public class FifoCacheController implements CacheController {
032:
033: private int cacheSize;
034: private Map cache;
035: private List keyList;
036:
037: /**
038: * Default constructor
039: */
040: public FifoCacheController() {
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: return cache.get(key);
091: }
092:
093: public Object removeObject(CacheModel cacheModel, Object key) {
094: keyList.remove(key);
095: return cache.remove(key);
096: }
097:
098: /**
099: * Flushes the cache.
100: *
101: * @param cacheModel The cache model
102: */
103: public void flush(CacheModel cacheModel) {
104: cache.clear();
105: keyList.clear();
106: }
107:
108: }
|