KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > cache > fifo > FifoCacheController


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.cache.fifo;
17
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import com.ibatis.sqlmap.engine.cache.CacheController;
26 import com.ibatis.sqlmap.engine.cache.CacheModel;
27
28 /**
29  * FIFO (first in, first out) cache controller implementation
30  */

31 public class FifoCacheController implements CacheController {
32
33   private int cacheSize;
34   private Map JavaDoc cache;
35   private List JavaDoc keyList;
36
37   /**
38    * Default constructor
39    */

40   public FifoCacheController() {
41     this.cacheSize = 100;
42     this.cache = Collections.synchronizedMap(new HashMap JavaDoc());
43     this.keyList = Collections.synchronizedList(new LinkedList JavaDoc());
44   }
45
46   /**
47    * Configures the cache
48    *
49    * @param props Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]
50    */

51   public void configure(Properties JavaDoc props) {
52     String JavaDoc size = props.getProperty("cache-size");
53     if (size == null) {
54       size = props.getProperty("size");
55     }
56     if (size != null) {
57       cacheSize = Integer.parseInt(size);
58     }
59   }
60
61   /**
62    * Add an object to the cache
63    *
64    * @param cacheModel The cacheModel
65    * @param key The key of the object to be cached
66    * @param value The object to be cached
67    */

68   public void putObject(CacheModel cacheModel, Object JavaDoc key, Object JavaDoc value) {
69     cache.put(key, value);
70     keyList.add(key);
71     if (keyList.size() > cacheSize) {
72       try {
73         Object JavaDoc oldestKey = keyList.remove(0);
74         cache.remove(oldestKey);
75       } catch (IndexOutOfBoundsException JavaDoc e) {
76         //ignore
77
}
78     }
79   }
80
81   /**
82    * Get an object out of the cache.
83    *
84    * @param cacheModel The cache model
85    * @param key The key of the object to be returned
86    * @return The cached object (or null)
87    */

88   public Object JavaDoc getObject(CacheModel cacheModel, Object JavaDoc key) {
89     return cache.get(key);
90   }
91
92   public Object JavaDoc removeObject(CacheModel cacheModel, Object JavaDoc key) {
93     keyList.remove(key);
94     return cache.remove(key);
95   }
96
97   /**
98    * Flushes the cache.
99    *
100    * @param cacheModel The cache model
101    */

102   public void flush(CacheModel cacheModel) {
103     cache.clear();
104     keyList.clear();
105   }
106
107 }
108
Popular Tags