public abstract class

AbstractAdaptiveCountingMemoryCache

extends Object
implements CountingMemoryCache<K, V>
java.lang.Object
   ↳ com.facebook.imagepipeline.cache.AbstractAdaptiveCountingMemoryCache<K, V>

Class Overview

Layer of memory cache stack responsible for managing eviction of the the cached items.

This layer is responsible for Adaptive Replacement Cache (ARC) eviction strategy and for maintaining the size boundaries of the cached items.

Only the exclusively owned elements, i.e. the elements not referenced by any client, can be evicted.

Summary

Fields
protected MemoryCacheParams mMemoryCacheParams
Public Constructors
AbstractAdaptiveCountingMemoryCache(Supplier<MemoryCacheParams> memoryCacheParamsSupplier, MemoryCache.CacheTrimStrategy cacheTrimStrategy, ValueDescriptor<V> valueDescriptor, int adaptiveRatePromil, int frequentlyUsedThreshold, int ghostListMaxSize, int lfuFractionPromil)
Public Methods
CloseableReference<V> cache(K key, CloseableReference<V> valueRef)
Caches the given key-value pair.
CloseableReference<V> cache(K key, CloseableReference<V> valueRef, EntryStateObserver<K> observer)
Caches the given key-value pair.
void clear()
Removes all the items from the cache.
synchronized boolean contains(K key)
Check if an item with the given cache key is currently in the cache.
synchronized boolean contains(Predicate<K> predicate)
Check if any items from the cache whose key matches the specified predicate.
CloseableReference<V> get(K key)
Gets the item with the given key, or null if there is no such item.
CountingLruMap getCachedEntries()
synchronized int getCount()
Gets the total number of all currently cached items.
synchronized int getEvictionQueueCount()
Gets the number of the exclusively owned items.
synchronized int getEvictionQueueSizeInBytes()
Gets the total size in bytes of the exclusively owned items.
synchronized int getInUseCount()
Gets the number of the cached items that are used by at least one client.
synchronized int getInUseSizeInBytes()
Gets the total size in bytes of the cached items that are used by at least one client.
MemoryCacheParams getMemoryCacheParams()
Map<Bitmap, Object> getOtherEntries()
synchronized int getSizeInBytes()
Gets the total size in bytes of all currently cached items.
void maybeEvictEntries()
Removes the exclusively owned items until the cache constraints are met.
void probe(K key)
Probes whether the object corresponding to the key is in the cache.
int removeAll(Predicate<K> predicate)
Removes all the items from the cache whose key matches the specified predicate.
String reportData()
CloseableReference<V> reuse(K key)
Gets the value with the given key to be reused, or null if there is no such value.
void trim(MemoryTrimType trimType)
Trims the cache according to the specified trimming strategy and the given trim type.
Protected Methods
abstract void logIllegalAdaptiveRate()
abstract void logIllegalLfuFraction()
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.facebook.cache.common.HasDebugData
From interface com.facebook.common.memory.MemoryTrimmable
From interface com.facebook.imagepipeline.cache.CountingMemoryCache
From interface com.facebook.imagepipeline.cache.MemoryCache

Fields

protected MemoryCacheParams mMemoryCacheParams

Public Constructors

public AbstractAdaptiveCountingMemoryCache (Supplier<MemoryCacheParams> memoryCacheParamsSupplier, MemoryCache.CacheTrimStrategy cacheTrimStrategy, ValueDescriptor<V> valueDescriptor, int adaptiveRatePromil, int frequentlyUsedThreshold, int ghostListMaxSize, int lfuFractionPromil)

Public Methods

public CloseableReference<V> cache (K key, CloseableReference<V> valueRef)

Caches the given key-value pair.

Important: the client should use the returned reference instead of the original one. It is the caller's responsibility to close the returned reference once not needed anymore.

Returns
  • the new reference to be used, null if the value cannot be cached

public CloseableReference<V> cache (K key, CloseableReference<V> valueRef, EntryStateObserver<K> observer)

Caches the given key-value pair.

Important: the client should use the returned reference instead of the original one. It is the caller's responsibility to close the returned reference once not needed anymore.

Returns
  • the new reference to be used, null if the value cannot be cached

public void clear ()

Removes all the items from the cache.

public synchronized boolean contains (K key)

Check if an item with the given cache key is currently in the cache.

Parameters
key returns true if an item with the given key matches
Returns
  • true is any items matches from the cache

public synchronized boolean contains (Predicate<K> predicate)

Check if any items from the cache whose key matches the specified predicate.

Parameters
predicate returns true if an item with the given key matches
Returns
  • true is any items matches from the cache

public CloseableReference<V> get (K key)

Gets the item with the given key, or null if there is no such item.

It is the caller's responsibility to close the returned reference once not needed anymore.

Returns
  • a reference to the cached value, or null if the item was not found

public CountingLruMap getCachedEntries ()

public synchronized int getCount ()

Gets the total number of all currently cached items.

public synchronized int getEvictionQueueCount ()

Gets the number of the exclusively owned items.

public synchronized int getEvictionQueueSizeInBytes ()

Gets the total size in bytes of the exclusively owned items.

public synchronized int getInUseCount ()

Gets the number of the cached items that are used by at least one client.

public synchronized int getInUseSizeInBytes ()

Gets the total size in bytes of the cached items that are used by at least one client.

public MemoryCacheParams getMemoryCacheParams ()

public Map<Bitmap, Object> getOtherEntries ()

public synchronized int getSizeInBytes ()

Gets the total size in bytes of all currently cached items.

public void maybeEvictEntries ()

Removes the exclusively owned items until the cache constraints are met.

This method invokes the external close() method, so it must not be called while holding the this lock.

public void probe (K key)

Probes whether the object corresponding to the key is in the cache. Note that the act of probing touches the item (if present in cache), thus changing its LRU timestamp.

public int removeAll (Predicate<K> predicate)

Removes all the items from the cache whose key matches the specified predicate.

Parameters
predicate returns true if an item with the given key should be removed
Returns
  • number of the items removed from the cache

public String reportData ()

public CloseableReference<V> reuse (K key)

Gets the value with the given key to be reused, or null if there is no such value.

The item can be reused only if it is exclusively owned by the cache.

public void trim (MemoryTrimType trimType)

Trims the cache according to the specified trimming strategy and the given trim type. We first trim the LFU cache, if we need to trim more, we continue to trim the MFU cache.

Protected Methods

protected abstract void logIllegalAdaptiveRate ()

protected abstract void logIllegalLfuFraction ()