@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 invoking Resource.dispose()
method. The consumer MUST periodically
call cleanResources()
method to trigger resource deallocation. The cache can be
permanently shut down using shutdown()
method. All resources associated with
the entries used by the cache will be deallocated.
This HttpCacheStorage
implementation is intended for use with FileResource
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 the close()
method
disposes only of those resources that have been explicitly removed from the cache with
removeEntry(String)
method.
The shutdown()
()} method can still be used to shut down the storage and dispose of
all resources currently managed by it.
Modifier and Type | Field and 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 and Description |
---|
ManagedHttpCacheStorage(CacheConfig config) |
Modifier and Type | Method and Description |
---|---|
void |
cleanResources() |
void |
close() |
private void |
ensureValidState() |
HttpCacheEntry |
getEntry(java.lang.String url)
Retrieves the cache entry stored under the given key
or null if no entry exists under that key.
|
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,
HttpCacheUpdateCallback callback)
Atomically applies the given callback to update an existing cache
entry under a given key.
|
private final CacheMap entries
private final java.lang.ref.ReferenceQueue<HttpCacheEntry> morque
private final java.util.Set<ResourceReference> resources
private final java.util.concurrent.atomic.AtomicBoolean active
public ManagedHttpCacheStorage(CacheConfig config)
private void ensureValidState() throws java.lang.IllegalStateException
java.lang.IllegalStateException
private void keepResourceReference(HttpCacheEntry entry)
public void putEntry(java.lang.String url, HttpCacheEntry entry) throws java.io.IOException
HttpCacheStorage
putEntry
in interface HttpCacheStorage
url
- where in the cache to store the entryentry
- cached response to storejava.io.IOException
public HttpCacheEntry getEntry(java.lang.String url) throws java.io.IOException
HttpCacheStorage
getEntry
in interface HttpCacheStorage
url
- cache keyHttpCacheEntry
or null
if no
entry existsjava.io.IOException
public void removeEntry(java.lang.String url) throws java.io.IOException
HttpCacheStorage
removeEntry
in interface HttpCacheStorage
java.io.IOException
public void updateEntry(java.lang.String url, HttpCacheUpdateCallback callback) throws java.io.IOException
HttpCacheStorage
updateEntry
in interface HttpCacheStorage
url
- indicates which entry to modifycallback
- performs the update; see
HttpCacheUpdateCallback
for details, but roughly the
callback expects to be handed the current entry and will return
the new value for the entry.java.io.IOException
public void cleanResources()
public void shutdown()
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable