src/java.base/share/classes/java/io/ExpiringCache.java
author rriggs
Mon, 19 Jun 2017 17:38:33 -0400
changeset 47415 354a527f3246
parent 47216 71c04702a3d5
permissions -rw-r--r--
8181597: Process Proxy presentation Reviewed-by: dfuchs, ahgross, rhalade, skoivu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11121
diff changeset
     2
 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.LinkedHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class ExpiringCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private long millisUntilExpiration;
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
    38
    private Map<String,Entry> map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // Clear out old entries every few queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private int queryCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private int queryOverflow = 300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int MAX_ENTRIES = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static class Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        private long   timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        private String val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Entry(long timestamp, String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            this.timestamp = timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            this.val = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        long   timestamp()                  { return timestamp;           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        void   setTimestamp(long timestamp) { this.timestamp = timestamp; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        String val()                        { return val;                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        void   setVal(String val)           { this.val = val;             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    ExpiringCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this(30000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
    64
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    ExpiringCache(long millisUntilExpiration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.millisUntilExpiration = millisUntilExpiration;
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    67
        map = new LinkedHashMap<>() {
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
    68
            protected boolean removeEldestEntry(Map.Entry<String,Entry> eldest) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
              return size() > MAX_ENTRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
          };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    synchronized String get(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (++queryCount >= queryOverflow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Entry entry = entryFor(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return entry.val();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    synchronized void put(String key, String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (++queryCount >= queryOverflow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Entry entry = entryFor(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            entry.setTimestamp(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            entry.setVal(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            map.put(key, new Entry(System.currentTimeMillis(), val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    synchronized void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        map.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private Entry entryFor(String key) {
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
   103
        Entry entry = map.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            long delta = System.currentTimeMillis() - entry.timestamp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (delta < 0 || delta >= millisUntilExpiration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                map.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private void cleanup() {
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
   115
        Set<String> keySet = map.keySet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Avoid ConcurrentModificationExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String[] keys = new String[keySet.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int i = 0;
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
   119
        for (String key: keySet) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            keys[i++] = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int j = 0; j < keys.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            entryFor(keys[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        queryCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}