Class ManagedHttpCacheStorage
- java.lang.Object
-
- org.apache.hc.client5.http.impl.cache.ManagedHttpCacheStorage
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,HttpCacheStorage
@Contract(threading=SAFE) public class ManagedHttpCacheStorage extends java.lang.Object implements HttpCacheStorage, java.io.Closeable
HttpCacheStorage
implementation capable of deallocating resources associated with the cache entries.This cache keeps track of cache entries using
PhantomReference
and maintains a collection of all resources that are no longer in use. The cache, however, does not automatically deallocates associated resources by invokingResource.dispose()
method. The consumer MUST periodically callcleanResources()
method to trigger resource deallocation. The cache can be permanently shut down usingshutdown()
method. All resources associated with the entries used by the cache will be deallocated.This
HttpCacheStorage
implementation is intended for use withFileResource
and similar.Compatibility note. Prior to version 4.4 this storage implementation used to dispose of all resource entries upon
close()
. As of version 4.4 theclose()
method disposes only of those resources that have been explicitly removed from the cache withremoveEntry(String)
method.The
shutdown()
()} method can still be used to shut down the storage and dispose of all resources currently managed by it.- Since:
- 4.1
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicBoolean
active
private CacheMap
entries
private java.lang.ref.ReferenceQueue<HttpCacheEntry>
morque
private java.util.Set<ResourceReference>
resources
-
Constructor Summary
Constructors Constructor Description ManagedHttpCacheStorage(CacheConfig config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cleanResources()
void
close()
private boolean
compareAndSet()
private void
ensureValidState()
java.util.Map<java.lang.String,HttpCacheEntry>
getEntries(java.util.Collection<java.lang.String> keys)
Retrieves multiple cache entries stored under the given keys.HttpCacheEntry
getEntry(java.lang.String url)
Retrieves the cache entry stored under the given key or null if no entry exists under that key.boolean
isActive()
Check if the cache is still active and has not shut down.private void
keepResourceReference(HttpCacheEntry entry)
void
putEntry(java.lang.String url, HttpCacheEntry entry)
Store a given cache entry under the given key.void
removeEntry(java.lang.String url)
Deletes/invalidates/removes any cache entries currently stored under the given key.void
shutdown()
void
updateEntry(java.lang.String url, HttpCacheCASOperation casOperation)
Atomically applies the given callback to processChallenge an existing cache entry under a given key.
-
-
-
Field Detail
-
entries
private final CacheMap entries
-
morque
private final java.lang.ref.ReferenceQueue<HttpCacheEntry> morque
-
resources
private final java.util.Set<ResourceReference> resources
-
active
private final java.util.concurrent.atomic.AtomicBoolean active
-
-
Constructor Detail
-
ManagedHttpCacheStorage
public ManagedHttpCacheStorage(CacheConfig config)
-
-
Method Detail
-
ensureValidState
private void ensureValidState()
-
keepResourceReference
private void keepResourceReference(HttpCacheEntry entry)
-
putEntry
public void putEntry(java.lang.String url, HttpCacheEntry entry) throws ResourceIOException
Description copied from interface:HttpCacheStorage
Store a given cache entry under the given key.- Specified by:
putEntry
in interfaceHttpCacheStorage
- Parameters:
url
- where in the cache to store the entryentry
- cached response to store- Throws:
ResourceIOException
-
getEntry
public HttpCacheEntry getEntry(java.lang.String url) throws ResourceIOException
Description copied from interface:HttpCacheStorage
Retrieves the cache entry stored under the given key or null if no entry exists under that key.- Specified by:
getEntry
in interfaceHttpCacheStorage
- Parameters:
url
- cache key- Returns:
- an
HttpCacheEntry
ornull
if no entry exists - Throws:
ResourceIOException
-
removeEntry
public void removeEntry(java.lang.String url) throws ResourceIOException
Description copied from interface:HttpCacheStorage
Deletes/invalidates/removes any cache entries currently stored under the given key.- Specified by:
removeEntry
in interfaceHttpCacheStorage
- Throws:
ResourceIOException
-
updateEntry
public void updateEntry(java.lang.String url, HttpCacheCASOperation casOperation) throws ResourceIOException
Description copied from interface:HttpCacheStorage
Atomically applies the given callback to processChallenge an existing cache entry under a given key.- Specified by:
updateEntry
in interfaceHttpCacheStorage
- Parameters:
url
- indicates which entry to modifycasOperation
- the CAS operation to perform.- Throws:
ResourceIOException
-
getEntries
public java.util.Map<java.lang.String,HttpCacheEntry> getEntries(java.util.Collection<java.lang.String> keys) throws ResourceIOException
Description copied from interface:HttpCacheStorage
Retrieves multiple cache entries stored under the given keys. Some implementations may use a single bulk operation to do the retrieval.- Specified by:
getEntries
in interfaceHttpCacheStorage
- Parameters:
keys
- cache keys- Returns:
- an map of
HttpCacheEntry
s. - Throws:
ResourceIOException
-
cleanResources
public void cleanResources()
-
shutdown
public void shutdown()
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
isActive
public boolean isActive()
Check if the cache is still active and has not shut down.- Returns:
true
if the cache is active, otherwise returnfalse
.- Since:
- 5.2
-
compareAndSet
private boolean compareAndSet()
-
-