< prev index next >

src/share/classes/sun/security/util/Cache.java

Print this page
rev 14405 : 8259886: Improve SSL session cache performance and scalability
Reviewed-by: erikj, xuelei
Contributed-by: djelinski <djelinski1@gmail.com>

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -250,10 +250,11 @@
     private final static boolean DEBUG = false;
 
     private final Map<K, CacheEntry<K,V>> cacheMap;
     private int maxSize;
     private long lifetime;
+    private long nextExpirationTime = Long.MAX_VALUE;
 
     // ReferenceQueue is of type V instead of Cache<K,V>
     // to allow SoftCacheEntry to extend SoftReference<V>
     private final ReferenceQueue<V> queue;
 

@@ -319,16 +320,22 @@
         if (lifetime == 0) {
             return;
         }
         int cnt = 0;
         long time = System.currentTimeMillis();
+        if (nextExpirationTime > time) {
+            return;
+        }
+        nextExpirationTime = Long.MAX_VALUE;
         for (Iterator<CacheEntry<K,V>> t = cacheMap.values().iterator();
                 t.hasNext(); ) {
             CacheEntry<K,V> entry = t.next();
             if (entry.isValid(time) == false) {
                 t.remove();
                 cnt++;
+            } else if (nextExpirationTime > entry.getExpirationTime()) {
+                nextExpirationTime = entry.getExpirationTime();
             }
         }
         if (DEBUG) {
             if (cnt != 0) {
                 System.out.println("Removed " + cnt

@@ -358,10 +365,13 @@
 
     public synchronized void put(K key, V value) {
         emptyQueue();
         long expirationTime = (lifetime == 0) ? 0 :
                                         System.currentTimeMillis() + lifetime;
+        if (expirationTime < nextExpirationTime) {
+            nextExpirationTime = expirationTime;
+        }
         CacheEntry<K,V> newEntry = newEntry(key, value, expirationTime, queue);
         CacheEntry<K,V> oldEntry = cacheMap.put(key, newEntry);
         if (oldEntry != null) {
             oldEntry.invalidate();
             return;

@@ -472,10 +482,11 @@
 
         K getKey();
 
         V getValue();
 
+        long getExpirationTime();
     }
 
     private static class HardCacheEntry<K,V> implements CacheEntry<K,V> {
 
         private K key;

@@ -494,10 +505,14 @@
 
         public V getValue() {
             return value;
         }
 
+        public long getExpirationTime() {
+            return expirationTime;
+        }
+
         public boolean isValid(long currentTime) {
             boolean valid = (currentTime <= expirationTime);
             if (valid == false) {
                 invalidate();
             }

@@ -531,10 +546,14 @@
 
         public V getValue() {
             return get();
         }
 
+        public long getExpirationTime() {
+            return expirationTime;
+        }
+
         public boolean isValid(long currentTime) {
             boolean valid = (currentTime <= expirationTime) && (get() != null);
             if (valid == false) {
                 invalidate();
             }
< prev index next >