Renamed HttpCacheUpdateCallback to HttpCacheCASOperation (CAS = Compare-And-Swap)
This commit is contained in:
parent
9581cbc7a0
commit
ebcb55d641
|
@ -27,12 +27,11 @@
|
|||
package org.apache.hc.client5.http.cache;
|
||||
|
||||
/**
|
||||
* Used for atomically updating entries in a {@link HttpCacheStorage}
|
||||
* implementation. The current entry (if any) is fed into an implementation
|
||||
* of this interface, and the new, possibly updated entry (if any)
|
||||
* should be returned.
|
||||
* Atomic Compare-And-Swap (CAS) cache operation.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
public interface HttpCacheUpdateCallback {
|
||||
public interface HttpCacheCASOperation {
|
||||
|
||||
/**
|
||||
* Returns the new cache entry that should replace an existing one.
|
||||
|
@ -42,9 +41,7 @@ public interface HttpCacheUpdateCallback {
|
|||
* {@code null} if nonexistent
|
||||
* @return the cache entry that should replace it, again,
|
||||
* possibly {@code null} if the entry should be deleted
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
HttpCacheEntry update(HttpCacheEntry existing) throws ResourceIOException;
|
||||
HttpCacheEntry execute(HttpCacheEntry existing) throws ResourceIOException;
|
||||
|
||||
}
|
|
@ -65,14 +65,11 @@ public interface HttpCacheStorage {
|
|||
* Atomically applies the given callback to processChallenge an existing cache
|
||||
* entry under a given key.
|
||||
* @param key indicates which entry to modify
|
||||
* @param callback performs the processChallenge; see
|
||||
* {@link HttpCacheUpdateCallback} for details, but roughly the
|
||||
* callback expects to be handed the current entry and will return
|
||||
* the new value for the entry.
|
||||
* @param casOperation the CAS operation to perform.
|
||||
* @throws ResourceIOException
|
||||
* @throws HttpCacheUpdateException
|
||||
*/
|
||||
void updateEntry(
|
||||
String key, HttpCacheUpdateCallback callback) throws ResourceIOException, HttpCacheUpdateException;
|
||||
String key, HttpCacheCASOperation casOperation) throws ResourceIOException, HttpCacheUpdateException;
|
||||
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ import org.apache.hc.client5.http.cache.HttpCacheEntry;
|
|||
import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheStorage;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheStorageEntry;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateException;
|
||||
import org.apache.hc.client5.http.cache.ResourceIOException;
|
||||
import org.apache.hc.core5.util.Args;
|
||||
|
@ -95,7 +95,7 @@ public abstract class AbstractSerializingCacheStorage<T, CAS> implements HttpCac
|
|||
@Override
|
||||
public final void updateEntry(
|
||||
final String key,
|
||||
final HttpCacheUpdateCallback callback) throws HttpCacheUpdateException, ResourceIOException {
|
||||
final HttpCacheCASOperation casOperation) throws HttpCacheUpdateException, ResourceIOException {
|
||||
int numRetries = 0;
|
||||
final String storageKey = digestToStorageKey(key);
|
||||
for (;;) {
|
||||
|
@ -105,7 +105,7 @@ public abstract class AbstractSerializingCacheStorage<T, CAS> implements HttpCac
|
|||
storageEntry = null;
|
||||
}
|
||||
final HttpCacheEntry existingEntry = storageEntry != null ? storageEntry.getContent() : null;
|
||||
final HttpCacheEntry updatedEntry = callback.update(existingEntry);
|
||||
final HttpCacheEntry updatedEntry = casOperation.execute(existingEntry);
|
||||
|
||||
if (existingEntry == null) {
|
||||
putEntry(key, updatedEntry);
|
||||
|
|
|
@ -35,7 +35,7 @@ import org.apache.hc.client5.http.cache.HeaderConstants;
|
|||
import org.apache.hc.client5.http.cache.HttpCacheEntry;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheInvalidator;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheStorage;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateException;
|
||||
import org.apache.hc.client5.http.cache.Resource;
|
||||
import org.apache.hc.client5.http.cache.ResourceFactory;
|
||||
|
@ -123,26 +123,22 @@ class BasicHttpCache implements HttpCache {
|
|||
final HttpHost target,
|
||||
final HttpRequest req,
|
||||
final HttpCacheEntry entry) throws ResourceIOException {
|
||||
final String parentURI = uriExtractor.generateKey(target, req);
|
||||
final String parentCacheKey = uriExtractor.generateKey(target, req);
|
||||
final String variantKey = uriExtractor.generateVariantKey(req, entry);
|
||||
final String variantURI = uriExtractor.generateVariantURI(target, req, entry);
|
||||
storage.putEntry(variantURI, entry);
|
||||
|
||||
final HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry update(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
return doGetUpdatedParentEntry(
|
||||
req.getRequestUri(), existing, entry,
|
||||
uriExtractor.generateVariantKey(req, entry),
|
||||
variantURI);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
try {
|
||||
storage.updateEntry(parentURI, callback);
|
||||
storage.updateEntry(parentCacheKey, new HttpCacheCASOperation() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry execute(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
return doGetUpdatedParentEntry(req.getRequestUri(), existing, entry, variantKey, variantURI);
|
||||
}
|
||||
|
||||
});
|
||||
} catch (final HttpCacheUpdateException e) {
|
||||
log.warn("Could not processChallenge key [" + parentURI + "]", e);
|
||||
log.warn("Could not processChallenge key [" + parentCacheKey + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,17 +150,15 @@ class BasicHttpCache implements HttpCache {
|
|||
final String variantKey = uriExtractor.generateVariantKey(req, entry);
|
||||
final String variantCacheKey = variant.getCacheKey();
|
||||
|
||||
final HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback() {
|
||||
@Override
|
||||
public HttpCacheEntry update(final HttpCacheEntry existing)
|
||||
throws ResourceIOException {
|
||||
return doGetUpdatedParentEntry(req.getRequestUri(),
|
||||
existing, entry, variantKey, variantCacheKey);
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
storage.updateEntry(parentCacheKey, callback);
|
||||
storage.updateEntry(parentCacheKey, new HttpCacheCASOperation() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry execute(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
return doGetUpdatedParentEntry(req.getRequestUri(), existing, entry, variantKey, variantCacheKey);
|
||||
}
|
||||
|
||||
});
|
||||
} catch (final HttpCacheUpdateException e) {
|
||||
log.warn("Could not processChallenge key [" + parentCacheKey + "]", e);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ package org.apache.hc.client5.http.impl.cache;
|
|||
|
||||
import org.apache.hc.client5.http.cache.HttpCacheEntry;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheStorage;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
|
||||
import org.apache.hc.client5.http.cache.ResourceIOException;
|
||||
import org.apache.hc.core5.annotation.Contract;
|
||||
import org.apache.hc.core5.annotation.ThreadingBehavior;
|
||||
|
@ -92,10 +92,9 @@ public class BasicHttpCacheStorage implements HttpCacheStorage {
|
|||
|
||||
@Override
|
||||
public synchronized void updateEntry(
|
||||
final String url,
|
||||
final HttpCacheUpdateCallback callback) throws ResourceIOException {
|
||||
final String url, final HttpCacheCASOperation casOperation) throws ResourceIOException {
|
||||
final HttpCacheEntry existingEntry = entries.get(url);
|
||||
entries.put(url, callback.update(existingEntry));
|
||||
entries.put(url, casOperation.execute(existingEntry));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||
|
||||
import org.apache.hc.client5.http.cache.HttpCacheEntry;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheStorage;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
|
||||
import org.apache.hc.client5.http.cache.Resource;
|
||||
import org.apache.hc.client5.http.cache.ResourceIOException;
|
||||
import org.apache.hc.core5.annotation.Contract;
|
||||
|
@ -136,13 +136,13 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
|
|||
@Override
|
||||
public void updateEntry(
|
||||
final String url,
|
||||
final HttpCacheUpdateCallback callback) throws ResourceIOException {
|
||||
final HttpCacheCASOperation casOperation) throws ResourceIOException {
|
||||
Args.notNull(url, "URL");
|
||||
Args.notNull(callback, "Callback");
|
||||
Args.notNull(casOperation, "CAS operation");
|
||||
ensureValidState();
|
||||
synchronized (this) {
|
||||
final HttpCacheEntry existing = this.entries.get(url);
|
||||
final HttpCacheEntry updated = callback.update(existing);
|
||||
final HttpCacheEntry updated = casOperation.execute(existing);
|
||||
this.entries.put(url, updated);
|
||||
if (existing != updated) {
|
||||
keepResourceReference(updated);
|
||||
|
|
|
@ -31,7 +31,7 @@ import java.util.Map;
|
|||
|
||||
import org.apache.hc.client5.http.cache.HttpCacheEntry;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheStorage;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
|
||||
import org.apache.hc.client5.http.cache.ResourceIOException;
|
||||
|
||||
class SimpleHttpCacheStorage implements HttpCacheStorage {
|
||||
|
@ -59,9 +59,9 @@ class SimpleHttpCacheStorage implements HttpCacheStorage {
|
|||
|
||||
@Override
|
||||
public void updateEntry(
|
||||
final String key, final HttpCacheUpdateCallback callback) throws ResourceIOException {
|
||||
final String key, final HttpCacheCASOperation casOperation) throws ResourceIOException {
|
||||
final HttpCacheEntry v1 = map.get(key);
|
||||
final HttpCacheEntry v2 = callback.update(v1);
|
||||
final HttpCacheEntry v2 = casOperation.execute(v1);
|
||||
map.put(key,v2);
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ import static org.mockito.Mockito.when;
|
|||
|
||||
import org.apache.hc.client5.http.cache.HttpCacheEntry;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheStorageEntry;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
|
||||
import org.apache.hc.client5.http.cache.HttpCacheUpdateException;
|
||||
import org.apache.hc.client5.http.cache.ResourceIOException;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
|
@ -135,10 +135,10 @@ public class TestAbstractSerializingCacheStorage {
|
|||
when(impl.digestToStorageKey(key)).thenReturn("bar");
|
||||
when(impl.getForUpdateCAS("bar")).thenReturn(null);
|
||||
|
||||
impl.updateEntry(key, new HttpCacheUpdateCallback() {
|
||||
impl.updateEntry(key, new HttpCacheCASOperation() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry update(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
public HttpCacheEntry execute(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
Assert.assertThat(existing, CoreMatchers.nullValue());
|
||||
return updatedValue;
|
||||
}
|
||||
|
@ -160,10 +160,10 @@ public class TestAbstractSerializingCacheStorage {
|
|||
when(impl.getStorageObject("stuff")).thenReturn(serialize(key, existingValue));
|
||||
when(impl.updateCAS(Mockito.eq("bar"), Mockito.eq("stuff"), Mockito.<byte[]>any())).thenReturn(true);
|
||||
|
||||
impl.updateEntry(key, new HttpCacheUpdateCallback() {
|
||||
impl.updateEntry(key, new HttpCacheCASOperation() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry update(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
public HttpCacheEntry execute(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
return updatedValue;
|
||||
}
|
||||
|
||||
|
@ -185,10 +185,10 @@ public class TestAbstractSerializingCacheStorage {
|
|||
when(impl.getStorageObject("stuff")).thenReturn(serialize("not-foo", existingValue));
|
||||
when(impl.updateCAS(Mockito.eq("bar"), Mockito.eq("stuff"), Mockito.<byte[]>any())).thenReturn(true);
|
||||
|
||||
impl.updateEntry(key, new HttpCacheUpdateCallback() {
|
||||
impl.updateEntry(key, new HttpCacheCASOperation() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry update(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
public HttpCacheEntry execute(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
Assert.assertThat(existing, CoreMatchers.nullValue());
|
||||
return updatedValue;
|
||||
}
|
||||
|
@ -211,10 +211,10 @@ public class TestAbstractSerializingCacheStorage {
|
|||
when(impl.getStorageObject("stuff")).thenReturn(serialize(key, existingValue));
|
||||
when(impl.updateCAS(Mockito.eq("bar"), Mockito.eq("stuff"), Mockito.<byte[]>any())).thenReturn(false, true);
|
||||
|
||||
impl.updateEntry(key, new HttpCacheUpdateCallback() {
|
||||
impl.updateEntry(key, new HttpCacheCASOperation() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry update(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
public HttpCacheEntry execute(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
return updatedValue;
|
||||
}
|
||||
|
||||
|
@ -237,10 +237,10 @@ public class TestAbstractSerializingCacheStorage {
|
|||
when(impl.updateCAS(Mockito.eq("bar"), Mockito.eq("stuff"), Mockito.<byte[]>any())).thenReturn(false, false, false, true);
|
||||
|
||||
try {
|
||||
impl.updateEntry(key, new HttpCacheUpdateCallback() {
|
||||
impl.updateEntry(key, new HttpCacheCASOperation() {
|
||||
|
||||
@Override
|
||||
public HttpCacheEntry update(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
public HttpCacheEntry execute(final HttpCacheEntry existing) throws ResourceIOException {
|
||||
return updatedValue;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue