0N/A/*
2362N/A * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2362N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/Aimport java.util.LinkedHashMap;
0N/Aimport java.util.Set;
0N/A
0N/Aclass ExpiringCache {
0N/A private long millisUntilExpiration;
0N/A private Map map;
0N/A // Clear out old entries every few queries
0N/A private int queryCount;
0N/A private int queryOverflow = 300;
0N/A private int MAX_ENTRIES = 200;
0N/A
0N/A static class Entry {
0N/A private long timestamp;
0N/A private String val;
0N/A
0N/A Entry(long timestamp, String val) {
0N/A this.timestamp = timestamp;
0N/A this.val = val;
0N/A }
0N/A
0N/A long timestamp() { return timestamp; }
0N/A void setTimestamp(long timestamp) { this.timestamp = timestamp; }
0N/A
0N/A String val() { return val; }
0N/A void setVal(String val) { this.val = val; }
0N/A }
0N/A
0N/A ExpiringCache() {
0N/A this(30000);
0N/A }
0N/A
0N/A ExpiringCache(long millisUntilExpiration) {
0N/A this.millisUntilExpiration = millisUntilExpiration;
0N/A map = new LinkedHashMap() {
0N/A protected boolean removeEldestEntry(Map.Entry eldest) {
0N/A return size() > MAX_ENTRIES;
0N/A }
0N/A };
}
synchronized String get(String key) {
if (++queryCount >= queryOverflow) {
cleanup();
}
Entry entry = entryFor(key);
if (entry != null) {
return entry.val();
}
return null;
}
synchronized void put(String key, String val) {
if (++queryCount >= queryOverflow) {
cleanup();
}
Entry entry = entryFor(key);
if (entry != null) {
entry.setTimestamp(System.currentTimeMillis());
entry.setVal(val);
} else {
map.put(key, new Entry(System.currentTimeMillis(), val));
}
}
synchronized void clear() {
map.clear();
}
private Entry entryFor(String key) {
Entry entry = (Entry) map.get(key);
if (entry != null) {
long delta = System.currentTimeMillis() - entry.timestamp();
if (delta < 0 || delta >= millisUntilExpiration) {
map.remove(key);
entry = null;
}
}
return entry;
}
private void cleanup() {
Set keySet = map.keySet();
// Avoid ConcurrentModificationExceptions
String[] keys = new String[keySet.size()];
int i = 0;
for (Iterator iter = keySet.iterator(); iter.hasNext(); ) {
String key = (String) iter.next();
keys[i++] = key;
}
for (int j = 0; j < keys.length; j++) {
entryFor(keys[j]);
}
queryCount = 0;
}
}