You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2023/08/30 19:15:12 UTC

[httpcomponents-client] 01/02: HTTPCLIENT-2284: internal cache storage improvements

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch HTTPCLIENT-2291
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit 478ae4fc69354606f8febfb101dfbda872922200
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Aug 26 16:52:26 2023 +0200

    HTTPCLIENT-2284: internal cache storage improvements
---
 .../http/impl/cache/BasicHttpCacheStorage.java     |  4 +-
 .../hc/client5/http/impl/cache/CacheMap.java       | 50 -------------
 .../http/impl/cache/InternalCacheStorage.java      | 85 ++++++++++++++++++++++
 .../http/impl/cache/ManagedHttpCacheStorage.java   |  4 +-
 4 files changed, 89 insertions(+), 54 deletions(-)

diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCacheStorage.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCacheStorage.java
index e02be0f4a..6e910bc0f 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCacheStorage.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCacheStorage.java
@@ -51,11 +51,11 @@ import org.apache.hc.core5.util.Args;
 @Contract(threading = ThreadingBehavior.SAFE)
 public class BasicHttpCacheStorage implements HttpCacheStorage {
 
-    private final CacheMap entries;
+    private final InternalCacheStorage entries;
 
     public BasicHttpCacheStorage(final CacheConfig config) {
         super();
-        this.entries = new CacheMap(config.getMaxCacheEntries());
+        this.entries = new InternalCacheStorage(config.getMaxCacheEntries(), null);
     }
 
     /**
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheMap.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheMap.java
deleted file mode 100644
index e610e740a..000000000
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheMap.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.hc.client5.http.impl.cache;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import org.apache.hc.client5.http.cache.HttpCacheEntry;
-
-final class CacheMap extends LinkedHashMap<String, HttpCacheEntry> {
-
-    private static final long serialVersionUID = -7750025207539768511L;
-
-    private final int maxEntries;
-
-    CacheMap(final int maxEntries) {
-        super(20, 0.75f, true);
-        this.maxEntries = maxEntries;
-    }
-
-    @Override
-    protected boolean removeEldestEntry(final Map.Entry<String, HttpCacheEntry> eldest) {
-        return size() > this.maxEntries;
-    }
-
-}
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/InternalCacheStorage.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/InternalCacheStorage.java
new file mode 100644
index 000000000..2fd352ca7
--- /dev/null
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/InternalCacheStorage.java
@@ -0,0 +1,85 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.impl.cache;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Queue;
+import java.util.function.Consumer;
+
+import org.apache.hc.client5.http.cache.HttpCacheEntry;
+import org.apache.hc.core5.annotation.Internal;
+
+@Internal
+final public class InternalCacheStorage {
+
+    private final Map<String, HttpCacheEntry> map;
+    private final Queue<HttpCacheEntry> evictionQueue;
+    private final Consumer<HttpCacheEntry> evictionCallback;
+
+    public InternalCacheStorage(final int maxEntries, final Consumer<HttpCacheEntry> evictionCallback) {
+        this.evictionCallback = evictionCallback;
+        this.map = new LinkedHashMap<String, HttpCacheEntry>(20, 0.75f, true) {
+
+            @Override
+            protected boolean removeEldestEntry(final Map.Entry<String, HttpCacheEntry> eldest) {
+                if (evictionCallback != null && size() > maxEntries) {
+                    evictionQueue.add(eldest.getValue());
+                    return true;
+                } else {
+                    return false;
+                }
+            }
+
+        };
+        this.evictionQueue = new LinkedList<>();
+    }
+
+    public void put(final String key, final HttpCacheEntry entry) {
+        map.put(key, entry);
+        HttpCacheEntry evicted;
+        while ((evicted = evictionQueue.poll()) != null) {
+            if (evictionCallback != null) {
+                evictionCallback.accept(evicted);
+            }
+        }
+    }
+
+    public HttpCacheEntry get(final String key) {
+        return map.get(key);
+    }
+
+    public HttpCacheEntry remove(final String key) {
+        return map.remove(key);
+    }
+
+    public void clear() {
+        map.clear();
+    }
+
+}
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ManagedHttpCacheStorage.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ManagedHttpCacheStorage.java
index 917e71d81..28546c095 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ManagedHttpCacheStorage.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ManagedHttpCacheStorage.java
@@ -77,14 +77,14 @@ import org.apache.hc.core5.util.Args;
 @Contract(threading = ThreadingBehavior.SAFE)
 public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
 
-    private final CacheMap entries;
+    private final InternalCacheStorage entries;
     private final ReferenceQueue<HttpCacheEntry> morque;
     private final Set<ResourceReference> resources;
     private final AtomicBoolean active;
 
     public ManagedHttpCacheStorage(final CacheConfig config) {
         super();
-        this.entries = new CacheMap(config.getMaxCacheEntries());
+        this.entries = new InternalCacheStorage(config.getMaxCacheEntries(), null);
         this.morque = new ReferenceQueue<>();
         this.resources = new HashSet<>();
         this.active = new AtomicBoolean(true);