public class

CountingMemoryCache

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

Class Overview

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

This layer is responsible for LRU 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

Nested Classes
interface CountingMemoryCache.CacheTrimStrategy Interface used to specify the trimming strategy for the cache. 
interface CountingMemoryCache.EntryStateObserver<K> Interface used to observe the state changes of an entry. 
Fields
protected MemoryCacheParams mMemoryCacheParams
Public Constructors
CountingMemoryCache(ValueDescriptor<V> valueDescriptor, CountingMemoryCache.CacheTrimStrategy cacheTrimStrategy, Supplier<MemoryCacheParams> memoryCacheParamsSupplier, PlatformBitmapFactory platformBitmapFactory, boolean isExternalCreatedBitmapLogEnabled)
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(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.
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.
synchronized int getSizeInBytes()
Gets the total size in bytes of all currently cached items.
int removeAll(Predicate<K> predicate)
Removes all the items from the cache whose key matches the specified predicate.
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.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.facebook.common.memory.MemoryTrimmable
From interface com.facebook.imagepipeline.cache.MemoryCache

Fields

protected MemoryCacheParams mMemoryCacheParams

Public Constructors

public CountingMemoryCache (ValueDescriptor<V> valueDescriptor, CountingMemoryCache.CacheTrimStrategy cacheTrimStrategy, Supplier<MemoryCacheParams> memoryCacheParamsSupplier, PlatformBitmapFactory platformBitmapFactory, boolean isExternalCreatedBitmapLogEnabled)

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 (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 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 synchronized int getSizeInBytes ()

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

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 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.