-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTPCLIENT-2284: internal cache storage improvements
- Loading branch information
Showing
5 changed files
with
195 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheMap.java
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
...ient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/InternalCacheStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* ==================================================================== | ||
* 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.client5.http.cache.HttpCacheStorageEntry; | ||
import org.apache.hc.core5.annotation.Internal; | ||
|
||
@Internal | ||
final public class InternalCacheStorage { | ||
|
||
private final Map<String, HttpCacheEntry> map; | ||
private final Queue<HttpCacheStorageEntry> evictionQueue; | ||
private final Consumer<HttpCacheStorageEntry> evictionCallback; | ||
|
||
public InternalCacheStorage(final int maxEntries, final Consumer<HttpCacheStorageEntry> 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(new HttpCacheStorageEntry(eldest.getKey(), eldest.getValue())); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
}; | ||
this.evictionQueue = new LinkedList<>(); | ||
} | ||
|
||
public InternalCacheStorage(final int maxEntries) { | ||
this(maxEntries, null); | ||
} | ||
|
||
public InternalCacheStorage() { | ||
this(Integer.MAX_VALUE, null); | ||
} | ||
|
||
public void put(final String key, final HttpCacheEntry entry) { | ||
map.put(key, entry); | ||
HttpCacheStorageEntry 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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestInternalCacheStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* ==================================================================== | ||
* 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.LinkedList; | ||
import java.util.Queue; | ||
|
||
import org.apache.hc.client5.http.cache.HttpCacheEntry; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestInternalCacheStorage { | ||
|
||
@Test | ||
public void testCacheBasics() { | ||
final InternalCacheStorage storage = new InternalCacheStorage(); | ||
final String key1 = "some-key-1"; | ||
Assertions.assertNull(storage.get(key1)); | ||
Assertions.assertNull(storage.remove(key1)); | ||
final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry(); | ||
storage.put(key1, entry1); | ||
Assertions.assertSame(entry1, storage.get(key1)); | ||
Assertions.assertSame(entry1, storage.remove(key1)); | ||
Assertions.assertNull(storage.get(key1)); | ||
Assertions.assertNull(storage.remove(key1)); | ||
final String key2 = "some-key-2"; | ||
final HttpCacheEntry entry2 = HttpTestUtils.makeCacheEntry(); | ||
final String key3 = "some-key-3"; | ||
final HttpCacheEntry entry3 = HttpTestUtils.makeCacheEntry(); | ||
storage.put(key2, entry2); | ||
storage.put(key3, entry3); | ||
Assertions.assertSame(entry2, storage.get(key2)); | ||
Assertions.assertSame(entry3, storage.get(key3)); | ||
storage.clear(); | ||
Assertions.assertNull(storage.get(key2)); | ||
Assertions.assertNull(storage.get(key3)); | ||
} | ||
|
||
@Test | ||
public void testCacheEviction() { | ||
final Queue<HttpCacheEntry> evictedEntries = new LinkedList<>(); | ||
final InternalCacheStorage storage = new InternalCacheStorage(2, e -> evictedEntries.add(e.getContent())); | ||
final String key1 = "some-key-1"; | ||
final String key2 = "some-key-2"; | ||
final String key3 = "some-key-3"; | ||
final String key4 = "some-key-4"; | ||
final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry(); | ||
final HttpCacheEntry entry2 = HttpTestUtils.makeCacheEntry(); | ||
final HttpCacheEntry entry3 = HttpTestUtils.makeCacheEntry(); | ||
final HttpCacheEntry entry4 = HttpTestUtils.makeCacheEntry(); | ||
|
||
storage.put(key1, entry1); | ||
storage.put(key2, entry2); | ||
storage.put(key3, entry3); | ||
storage.put(key4, entry4); | ||
Assertions.assertSame(entry1, evictedEntries.poll()); | ||
Assertions.assertSame(entry2, evictedEntries.poll()); | ||
Assertions.assertNull(evictedEntries.poll()); | ||
|
||
storage.clear(); | ||
storage.put(key1, entry1); | ||
storage.put(key2, entry2); | ||
storage.get(key1); | ||
storage.put(key3, entry3); | ||
storage.get(key1); | ||
storage.put(key4, entry4); | ||
|
||
Assertions.assertSame(entry2, evictedEntries.poll()); | ||
Assertions.assertSame(entry3, evictedEntries.poll()); | ||
Assertions.assertNull(evictedEntries.poll()); | ||
} | ||
|
||
} |