jdk/src/share/classes/sun/security/util/Cache.java
author alanb
Mon, 05 Oct 2009 16:45:55 +0100
changeset 3956 2586d23078e4
parent 2060 75e464ce81af
child 5506 202f599c92aa
permissions -rw-r--r--
6854954: Eliminate static dependency on java.awt.AWTPermission Reviewed-by: mullan, mchung, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
     2
 * Copyright 2002-2009 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Abstract base class and factory for caches. A cache is a key-value mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * It has properties that make it more suitable for caching than a Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The factory methods can be used to obtain two different implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * They have the following properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  . keys and values reside in memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  . keys and values must be non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  . maximum size. Replacements are made in LRU order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  . optional lifetime, specified in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *  . save for concurrent use by multiple threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  . values are held by either standard references or via SoftReferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    SoftReferences have the advantage that they are automatically cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    by the garbage collector in response to memory demand. This makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    possible to simple set the maximum size to a very large value and let
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *    the GC automatically size the cache dynamically depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    amount of available memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * However, note that because of the way SoftReferences are implemented in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * HotSpot at the moment, this may not work perfectly as it clears them fairly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * eagerly. Performance may be improved if the Java heap size is set to larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * value using e.g. java -ms64M -mx128M foo.Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Cache sizing: the memory cache is implemented on top of a LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * In its current implementation, the number of buckets (NOT entries) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * (Linked)HashMaps is always a power of two. It is recommended to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * maximum cache size to value that uses those buckets fully. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * if a cache with somewhere between 500 and 1000 entries is desired, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * maximum size of 750 would be a good choice: try 1024 buckets, with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * load factor of 0.75f, the number of entries can be calculated as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * buckets / 4 * 3. As mentioned above, with a SoftReference cache, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * generally reasonable to set the size to a fairly large value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public abstract class Cache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected Cache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Return the number of currently valid entries in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public abstract int size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Remove all entries from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public abstract void clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Add an entry to the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public abstract void put(Object key, Object value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Get a value from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public abstract Object get(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Remove an entry from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public abstract void remove(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   104
     * Set the maximum size.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   105
     */
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   106
    public abstract void setCapacity(int size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   107
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   108
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   109
     * Set the timeout(in seconds).
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   110
     */
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   111
    public abstract void setTimeout(int timeout);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   112
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   113
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   114
     * accept a visitor
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   115
     */
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   116
    public abstract void accept(CacheVisitor visitor);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   117
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   118
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Return a new memory cache with the specified maximum size, unlimited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * lifetime for entries, with the values held by SoftReferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static Cache newSoftMemoryCache(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return new MemoryCache(true, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Return a new memory cache with the specified maximum size, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * specified maximum lifetime (in seconds), with the values held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * by SoftReferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static Cache newSoftMemoryCache(int size, int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return new MemoryCache(true, size, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Return a new memory cache with the specified maximum size, unlimited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * lifetime for entries, with the values held by standard references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static Cache newHardMemoryCache(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return new MemoryCache(false, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Return a dummy cache that does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static Cache newNullCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return NullCache.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Return a new memory cache with the specified maximum size, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * specified maximum lifetime (in seconds), with the values held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * by standard references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static Cache newHardMemoryCache(int size, int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return new MemoryCache(false, size, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Utility class that wraps a byte array and implements the equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * and hashCode() contract in a way suitable for Maps and caches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static class EqualByteArray {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        private final byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        private volatile int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public EqualByteArray(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            this.b = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            int h = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (h == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                h = b.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    h += (b[i] & 0xff) * 37;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                hash = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (obj instanceof EqualByteArray == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            EqualByteArray other = (EqualByteArray)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return Arrays.equals(this.b, other.b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   196
    public interface CacheVisitor {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   197
        public void visit(Map<Object, Object> map);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   198
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   199
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
class NullCache extends Cache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    final static Cache INSTANCE = new NullCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private NullCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void put(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   230
    public void setCapacity(int size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   231
        // empty
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   232
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   233
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   234
    public void setTimeout(int timeout) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   235
        // empty
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   236
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   237
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   238
    public void accept(CacheVisitor visitor) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   239
        // empty
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   240
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   241
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
class MemoryCache extends Cache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private final static float LOAD_FACTOR = 0.75f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    // XXXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private final static boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private final Map<Object, CacheEntry> cacheMap;
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   252
    private int maxSize;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   253
    private long lifetime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private final ReferenceQueue queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public MemoryCache(boolean soft, int maxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        this(soft, maxSize, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public MemoryCache(boolean soft, int maxSize, int lifetime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        this.maxSize = maxSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        this.lifetime = lifetime * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        this.queue = soft ? new ReferenceQueue() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int buckets = (int)(maxSize / LOAD_FACTOR) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        cacheMap = new LinkedHashMap<Object, CacheEntry>(buckets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                        LOAD_FACTOR, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Empty the reference queue and remove all corresponding entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * This method should be called at the beginning of each public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private void emptyQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (queue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int startSize = cacheMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            CacheEntry entry = (CacheEntry)queue.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            Object key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                // key is null, entry has already been removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            CacheEntry currentEntry = cacheMap.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // check if the entry in the map corresponds to the expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // entry. If not, readd the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if ((currentEntry != null) && (entry != currentEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                cacheMap.put(key, currentEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            int endSize = cacheMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (startSize != endSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                System.out.println("*** Expunged " + (startSize - endSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        + " entries, " + endSize + " entries left");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Scan all entries and remove all expired ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private void expungeExpiredEntries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        emptyQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (lifetime == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        for (Iterator<CacheEntry> t = cacheMap.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                t.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            CacheEntry entry = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (entry.isValid(time) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                t.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (cnt != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                System.out.println("Removed " + cnt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        + " expired entries, remaining " + cacheMap.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public synchronized int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        expungeExpiredEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return cacheMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public synchronized void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (queue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // if this is a SoftReference cache, first invalidate() all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // entries so that GC does not have to enqueue them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            for (CacheEntry entry : cacheMap.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                entry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            while (queue.poll() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        cacheMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public synchronized void put(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        emptyQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        long expirationTime = (lifetime == 0) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                        System.currentTimeMillis() + lifetime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        CacheEntry newEntry = newEntry(key, value, expirationTime, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        CacheEntry oldEntry = cacheMap.put(key, newEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (oldEntry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            oldEntry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   362
        if (maxSize > 0 && cacheMap.size() > maxSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            expungeExpiredEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (cacheMap.size() > maxSize) { // still too large?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                Iterator<CacheEntry> t = cacheMap.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                CacheEntry lruEntry = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    System.out.println("** Overflow removal "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        + lruEntry.getKey() + " | " + lruEntry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                t.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                lruEntry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public synchronized Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        emptyQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        CacheEntry entry = cacheMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        long time = (lifetime == 0) ? 0 : System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (entry.isValid(time) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                System.out.println("Ignoring expired entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            cacheMap.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public synchronized void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        emptyQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        CacheEntry entry = cacheMap.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            entry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   402
    public synchronized void setCapacity(int size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   403
        expungeExpiredEntries();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   404
        if (size > 0 && cacheMap.size() > size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   405
            Iterator<CacheEntry> t = cacheMap.values().iterator();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   406
            for (int i = cacheMap.size() - size; i > 0; i--) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   407
                CacheEntry lruEntry = t.next();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   408
                if (DEBUG) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   409
                    System.out.println("** capacity reset removal "
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   410
                        + lruEntry.getKey() + " | " + lruEntry.getValue());
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   411
                }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   412
                t.remove();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   413
                lruEntry.invalidate();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   414
            }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   415
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   416
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   417
        maxSize = size > 0 ? size : 0;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   418
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   419
        if (DEBUG) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   420
            System.out.println("** capacity reset to " + size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   421
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   422
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   423
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   424
    public synchronized void setTimeout(int timeout) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   425
        emptyQueue();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   426
        lifetime = timeout > 0 ? timeout * 1000L : 0L;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   427
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   428
        if (DEBUG) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   429
            System.out.println("** lifetime reset to " + timeout);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   430
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   431
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   432
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   433
    // it is a heavyweight method.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   434
    public synchronized void accept(CacheVisitor visitor) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   435
        expungeExpiredEntries();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   436
        Map<Object, Object> cached = getCachedEntries();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   437
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   438
        visitor.visit(cached);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   439
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   440
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   441
    private Map<Object, Object> getCachedEntries() {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   442
        Map<Object,Object> kvmap = new HashMap<Object,Object>(cacheMap.size());
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   443
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   444
        for (CacheEntry entry : cacheMap.values()) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   445
            kvmap.put(entry.getKey(), entry.getValue());
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   446
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   447
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   448
        return kvmap;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   449
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   450
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    protected CacheEntry newEntry(Object key, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            long expirationTime, ReferenceQueue queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (queue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return new SoftCacheEntry(key, value, expirationTime, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return new HardCacheEntry(key, value, expirationTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private static interface CacheEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        boolean isValid(long currentTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        void invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        Object getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        Object getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    private static class HardCacheEntry implements CacheEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        private Object key, value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        private long expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        HardCacheEntry(Object key, Object value, long expirationTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            this.expirationTime = expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        public Object getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        public boolean isValid(long currentTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            boolean valid = (currentTime <= expirationTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (valid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            expirationTime = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private static class SoftCacheEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            extends SoftReference implements CacheEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        private Object key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        private long expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        SoftCacheEntry(Object key, Object value, long expirationTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                ReferenceQueue queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            super(value, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            this.expirationTime = expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        public Object getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        public boolean isValid(long currentTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            boolean valid = (currentTime <= expirationTime) && (get() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (valid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            expirationTime = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
}