Path: blob/master/src/java.base/share/classes/java/io/ExpiringCache.java
41152 views
/*1* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*/2728package java.io;2930import java.util.Iterator;31import java.util.Map;32import java.util.LinkedHashMap;33import java.util.Set;3435class ExpiringCache {36private long millisUntilExpiration;37private Map<String,Entry> map;38// Clear out old entries every few queries39private int queryCount;40private int queryOverflow = 300;41private int MAX_ENTRIES = 200;4243static class Entry {44private long timestamp;45private String val;4647Entry(long timestamp, String val) {48this.timestamp = timestamp;49this.val = val;50}5152long timestamp() { return timestamp; }53void setTimestamp(long timestamp) { this.timestamp = timestamp; }5455String val() { return val; }56void setVal(String val) { this.val = val; }57}5859ExpiringCache() {60this(30000);61}6263@SuppressWarnings("serial")64ExpiringCache(long millisUntilExpiration) {65this.millisUntilExpiration = millisUntilExpiration;66map = new LinkedHashMap<>() {67protected boolean removeEldestEntry(Map.Entry<String,Entry> eldest) {68return size() > MAX_ENTRIES;69}70};71}7273synchronized String get(String key) {74if (++queryCount >= queryOverflow) {75cleanup();76}77Entry entry = entryFor(key);78if (entry != null) {79return entry.val();80}81return null;82}8384synchronized void put(String key, String val) {85if (++queryCount >= queryOverflow) {86cleanup();87}88Entry entry = entryFor(key);89if (entry != null) {90entry.setTimestamp(System.currentTimeMillis());91entry.setVal(val);92} else {93map.put(key, new Entry(System.currentTimeMillis(), val));94}95}9697synchronized void clear() {98map.clear();99}100101private Entry entryFor(String key) {102Entry entry = map.get(key);103if (entry != null) {104long delta = System.currentTimeMillis() - entry.timestamp();105if (delta < 0 || delta >= millisUntilExpiration) {106map.remove(key);107entry = null;108}109}110return entry;111}112113private void cleanup() {114Set<String> keySet = map.keySet();115// Avoid ConcurrentModificationExceptions116String[] keys = new String[keySet.size()];117int i = 0;118for (String key: keySet) {119keys[i++] = key;120}121for (int j = 0; j < keys.length; j++) {122entryFor(keys[j]);123}124queryCount = 0;125}126}127128129