51 |
51 |
*/
|
52 |
52 |
package org.gvsig.raster.cache.tile.impl.memory;
|
53 |
53 |
|
54 |
|
import java.util.HashMap;
|
55 |
|
import java.util.LinkedList;
|
56 |
|
import java.util.logging.Level;
|
57 |
|
import java.util.logging.Logger;
|
58 |
54 |
|
59 |
|
import org.gvsig.raster.cache.buffer.Buffer;
|
|
55 |
//import org.gvsig.raster.cache.buffer.Buffer;
|
60 |
56 |
|
61 |
57 |
/**
|
62 |
58 |
* A Last Recent Used cache of Bitmaps
|
... | ... | |
64 |
60 |
* @author rblanco
|
65 |
61 |
*
|
66 |
62 |
*/
|
67 |
|
public class LRUTileCache extends HashMap<String, Buffer> {
|
68 |
|
private static final long serialVersionUID = 6215141;
|
69 |
|
private final int maxCacheSize;
|
70 |
|
private LinkedList<String> list = null;
|
71 |
|
private final static Logger log = Logger.getLogger("LRUTileCache");
|
72 |
|
|
73 |
|
/**
|
74 |
|
* The Constructor
|
75 |
|
* @param maxCacheSize The cache size in number of Bitmaps
|
76 |
|
*/
|
77 |
|
public LRUTileCache(final int maxCacheSize) {
|
78 |
|
super(maxCacheSize);
|
79 |
|
this.maxCacheSize = Math.max(0, maxCacheSize);
|
80 |
|
this.list = new LinkedList<String>();
|
81 |
|
}
|
82 |
|
|
83 |
|
/**
|
84 |
|
* Clears the cache
|
85 |
|
*/
|
86 |
|
public synchronized void clear() {
|
87 |
|
try {
|
88 |
|
/*for (final Buffer b : this.values()) {
|
89 |
|
if (b != null) {
|
90 |
|
b.recycle();
|
91 |
|
}
|
92 |
|
}*/
|
93 |
|
super.clear();
|
94 |
|
list.clear();
|
95 |
|
} catch (Exception e) {
|
96 |
|
log.log(Level.SEVERE, "clear cache: ", e);
|
97 |
|
}
|
98 |
|
}
|
99 |
|
|
100 |
|
/**
|
101 |
|
* Adds a Bitmap to the cache. If the cache is full, removes the last
|
102 |
|
*/
|
103 |
|
public synchronized Buffer put(final String key, final Buffer value) {
|
104 |
|
try {
|
105 |
|
if (maxCacheSize == 0) {
|
106 |
|
return null;
|
107 |
|
}
|
108 |
|
|
109 |
|
// if the key isn't in the cache and the cache is full...
|
110 |
|
if (!super.containsKey(key) && !list.isEmpty()
|
111 |
|
&& list.size() + 1 > maxCacheSize) {
|
112 |
|
final Object deadKey = list.removeLast();
|
113 |
|
super.remove(deadKey);
|
114 |
|
//if (bitmap != null)
|
115 |
|
//bitmap.recycle();
|
116 |
|
}
|
117 |
|
|
118 |
|
updateKey(key);
|
119 |
|
} catch (Exception e) {
|
120 |
|
log.log(Level.SEVERE, "put", e);
|
121 |
|
}
|
122 |
|
return super.put(key, value);
|
123 |
|
}
|
124 |
|
|
125 |
|
/**
|
126 |
|
* Returns a cached Bitmap given a key
|
127 |
|
* @param key The key of the Bitmap stored on the HashMap
|
128 |
|
* @return The Bitmap or null if it is not stored in the cache
|
129 |
|
*/
|
130 |
|
public synchronized Buffer get(final String key) {
|
131 |
|
try {
|
132 |
|
final Buffer value = super.get(key);
|
133 |
|
if (value != null) {
|
134 |
|
updateKey(key);
|
135 |
|
}
|
136 |
|
return value;
|
137 |
|
} catch (Exception e) {
|
138 |
|
log.log(Level.SEVERE, "get", e);
|
139 |
|
return null;
|
140 |
|
}
|
141 |
|
}
|
142 |
|
|
143 |
|
/**
|
144 |
|
* Removes a Bitmap from the cache
|
145 |
|
* @param key The key of the Bitmap to remove
|
146 |
|
*/
|
147 |
|
public synchronized void remove(final String key) {
|
148 |
|
try {
|
149 |
|
list.remove(key);
|
150 |
|
} catch (Exception e) {
|
151 |
|
log.log(Level.SEVERE, "remove", e);
|
152 |
|
}
|
153 |
|
super.remove(key);
|
154 |
|
//if (bitmap != null)
|
155 |
|
//bitmap.recycle();
|
156 |
|
}
|
157 |
|
|
158 |
|
/**
|
159 |
|
* The key is touched (recent used) and added to the top of the list
|
160 |
|
* @param key The key to be updated
|
161 |
|
*/
|
162 |
|
private void updateKey(final String key) {
|
163 |
|
try {
|
164 |
|
list.remove(key);
|
165 |
|
list.addFirst(key);
|
166 |
|
} catch (Exception e) {
|
167 |
|
log.log(Level.SEVERE, "updatekey", e);
|
168 |
|
}
|
169 |
|
}
|
|
63 |
public class LRUTileCache {
|
|
64 |
// extends HashMap<String, Buffer> {
|
|
65 |
// private static final long serialVersionUID = 6215141;
|
|
66 |
// private final int maxCacheSize;
|
|
67 |
// private LinkedList<String> list = null;
|
|
68 |
// private final static Logger log = Logger.getLogger("LRUTileCache");
|
|
69 |
//
|
|
70 |
// /**
|
|
71 |
// * The Constructor
|
|
72 |
// * @param maxCacheSize The cache size in number of Bitmaps
|
|
73 |
// */
|
|
74 |
// public LRUTileCache(final int maxCacheSize) {
|
|
75 |
// super(maxCacheSize);
|
|
76 |
// this.maxCacheSize = Math.max(0, maxCacheSize);
|
|
77 |
// this.list = new LinkedList<String>();
|
|
78 |
// }
|
|
79 |
//
|
|
80 |
// /**
|
|
81 |
// * Clears the cache
|
|
82 |
// */
|
|
83 |
// public synchronized void clear() {
|
|
84 |
// try {
|
|
85 |
// /*for (final Buffer b : this.values()) {
|
|
86 |
// if (b != null) {
|
|
87 |
// b.recycle();
|
|
88 |
// }
|
|
89 |
// }*/
|
|
90 |
// super.clear();
|
|
91 |
// list.clear();
|
|
92 |
// } catch (Exception e) {
|
|
93 |
// log.log(Level.SEVERE, "clear cache: ", e);
|
|
94 |
// }
|
|
95 |
// }
|
|
96 |
//
|
|
97 |
// /**
|
|
98 |
// * Adds a Bitmap to the cache. If the cache is full, removes the last
|
|
99 |
// */
|
|
100 |
// public synchronized Buffer put(final String key, final Buffer value) {
|
|
101 |
// try {
|
|
102 |
// if (maxCacheSize == 0) {
|
|
103 |
// return null;
|
|
104 |
// }
|
|
105 |
//
|
|
106 |
// // if the key isn't in the cache and the cache is full...
|
|
107 |
// if (!super.containsKey(key) && !list.isEmpty()
|
|
108 |
// && list.size() + 1 > maxCacheSize) {
|
|
109 |
// final Object deadKey = list.removeLast();
|
|
110 |
// super.remove(deadKey);
|
|
111 |
// //if (bitmap != null)
|
|
112 |
// //bitmap.recycle();
|
|
113 |
// }
|
|
114 |
//
|
|
115 |
// updateKey(key);
|
|
116 |
// } catch (Exception e) {
|
|
117 |
// log.log(Level.SEVERE, "put", e);
|
|
118 |
// }
|
|
119 |
// return super.put(key, value);
|
|
120 |
// }
|
|
121 |
//
|
|
122 |
// /**
|
|
123 |
// * Returns a cached Bitmap given a key
|
|
124 |
// * @param key The key of the Bitmap stored on the HashMap
|
|
125 |
// * @return The Bitmap or null if it is not stored in the cache
|
|
126 |
// */
|
|
127 |
// public synchronized Buffer get(final String key) {
|
|
128 |
// try {
|
|
129 |
// final Buffer value = super.get(key);
|
|
130 |
// if (value != null) {
|
|
131 |
// updateKey(key);
|
|
132 |
// }
|
|
133 |
// return value;
|
|
134 |
// } catch (Exception e) {
|
|
135 |
// log.log(Level.SEVERE, "get", e);
|
|
136 |
// return null;
|
|
137 |
// }
|
|
138 |
// }
|
|
139 |
//
|
|
140 |
// /**
|
|
141 |
// * Removes a Bitmap from the cache
|
|
142 |
// * @param key The key of the Bitmap to remove
|
|
143 |
// */
|
|
144 |
// public synchronized void remove(final String key) {
|
|
145 |
// try {
|
|
146 |
// list.remove(key);
|
|
147 |
// } catch (Exception e) {
|
|
148 |
// log.log(Level.SEVERE, "remove", e);
|
|
149 |
// }
|
|
150 |
// super.remove(key);
|
|
151 |
// //if (bitmap != null)
|
|
152 |
// //bitmap.recycle();
|
|
153 |
// }
|
|
154 |
//
|
|
155 |
// /**
|
|
156 |
// * The key is touched (recent used) and added to the top of the list
|
|
157 |
// * @param key The key to be updated
|
|
158 |
// */
|
|
159 |
// private void updateKey(final String key) {
|
|
160 |
// try {
|
|
161 |
// list.remove(key);
|
|
162 |
// list.addFirst(key);
|
|
163 |
// } catch (Exception e) {
|
|
164 |
// log.log(Level.SEVERE, "updatekey", e);
|
|
165 |
// }
|
|
166 |
// }
|
170 |
167 |
}
|