KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > cache > lru > LruCacheController


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.lru;
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  * LRU (least recently used) cache controller implementation
30  */

31 public class LruCacheController 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 LruCacheController() {
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     Object JavaDoc result = cache.get(key);
90     keyList.remove(key);
91     if (result != null) {
92       keyList.add(key);
93     }
94     return result;
95   }
96
97   public Object JavaDoc removeObject(CacheModel cacheModel, Object JavaDoc key) {
98     keyList.remove(key);
99     return cache.remove(key);
100   }
101
102   /**
103    * Flushes the cache.
104    *
105    * @param cacheModel The cache model
106    */

107   public void flush(CacheModel cacheModel) {
108     cache.clear();
109     keyList.clear();
110   }
111
112 }
113
Popular Tags