T - type of entry stored in the cachepublic class MessageCache<T> extends Object
MessageCache.Weigher to compute the weight of each entry
being stored inside the cache.
- Hard limit. This is the upper bound weight for the cache and it won't grow beyond this.
- Min retain. This is the minimum weight that the cache will try to maintain.
- Reduce trigger. This is the cache weight that triggers the logic for reducing the cache size back to the
min retain weight. When the cache weight is larger than this limit, weight reduction logic will be executed
by the consumer. On adding entries to the cache, the cache can keep growing without blocking as long as
the hard limit is not hit so that the publisher doesn't need to be blocked.
The room between the reduce trigger and hard limits is basically the buffer for non-blocking addition.
- On addition, once the hard limit is reached, a blocking operation is needed to reduce the weight of the
cache back to min retain.
- On fetching entries from the cache, the fetcher will check whether it needs to reduce the cache weight and reduce
it if needed. This essentially is to amortize the cost of the blocking weight reduction operations among all
fetchers (which typically has multiple of them), without blocking the single publish as much as possible.| Modifier and Type | Class and Description |
|---|---|
static class |
MessageCache.EntryUpdater<T>
A updater for updating an entry.
|
static class |
MessageCache.Limits
Carries the limits for the
MessageCache. |
static interface |
MessageCache.Scanner<T>
This interface is for accessing cached entries.
|
static interface |
MessageCache.Weigher<T>
This interface is for calculating the weight of a cache entry.
|
| Constructor and Description |
|---|
MessageCache(Comparator<T> comparator,
MessageCache.Weigher<T> weigher,
MessageCache.Limits limits,
co.cask.cdap.api.metrics.MetricsContext metricsContext)
Creates a new instance of the cache.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addAll(Iterator<T> entries)
Adds a list of entries to the cache.
|
void |
clear()
Clears the cache.
|
Comparator<T> |
getComparator()
Returns the
Comparator used by this cache. |
MessageCache.Limits |
getLimits()
Returns the current soft and hard limits of this cache.
|
void |
resize(MessageCache.Limits limits)
Resize the cache limits.
|
MessageCache.Scanner<T> |
scan(T startEntry,
boolean includeStart,
int limit,
MessageFilter<T> filter)
Creates a
MessageCache.Scanner for fetching cached entries in ascending order. |
void |
updateEntries(T startEntry,
T endEntry,
MessageCache.EntryUpdater<T> updater)
Updates entries in the cache.
|
public MessageCache(Comparator<T> comparator, MessageCache.Weigher<T> weigher, MessageCache.Limits limits, co.cask.cdap.api.metrics.MetricsContext metricsContext)
comparator - a Comparator for ordering cache entriesweigher - a MessageCache.Weigher for computing the weight of each cache entrylimits - the limits for maintaining cache weight; see class description for more detailmetricsContext - a MetricsContext for emitting metrics about this cache.public Comparator<T> getComparator()
Comparator used by this cache.public void addAll(Iterator<T> entries)
entries - a Iterator to provide entries to be added to the cacheConcurrentModificationException - if called by multiple threads concurrentlyIllegalArgumentException - if the entries provided are not in strictly increasing order
or not larger existing cached entriespublic MessageCache.Scanner<T> scan(T startEntry, boolean includeStart, int limit, MessageFilter<T> filter)
MessageCache.Scanner for fetching cached entries in ascending order.startEntry - the entry to start fetching fromincludeStart - true to include the startEntry in the resulting MessageCache.Scanner
if it exists in the cachelimit - maximum number of entries to fetchMessageCache.Scanner for accessing to the fetched entriespublic void updateEntries(T startEntry, T endEntry, MessageCache.EntryUpdater<T> updater)
Comparator provided to this cache.startEntry - the starting entry for the update to start (inclusive)endEntry - the ending entry for the update to end (inclusive)updater - a MessageCache.EntryUpdater to update the content of a entrypublic void clear()
addAll(Iterator) method.public void resize(MessageCache.Limits limits)
limits - the new limits for this cache.public MessageCache.Limits getLimits()
Copyright © 2017 Cask Data, Inc. Licensed under the Apache License, Version 2.0.