org.terracotta.cache
Class LocalCache<K,V>

java.lang.Object
  extended by org.terracotta.cache.LocalCache<K,V>
Type Parameters:
K - key type
V - value type
All Implemented Interfaces:
ConcurrentMap<K,V>, Map<K,V>, DistributedCache<K,V>, Evictable<K>, LockableMap<K>

public class LocalCache<K,V>
extends Object
implements DistributedCache<K,V>, Evictable<K>

A local (non-clustered) cache implementation.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  ConcurrentDistributedMap<K,TimestampedValue<V>> data
           
 
Constructor Summary
LocalCache(CacheConfig config)
          Construct a cache with the given configuration
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 ClusteredLock createFinegrainedLock(K key)
          Obtain a fine-grained lock instance for a particular key.
 Set<Map.Entry<K,V>> entrySet()
           
 void evictExpiredLocalElements()
          Trigger to walk through the local elements, look for expired elements, and evict them.
 void evictOrphanElements(ClusterInfo cluster)
          Trigger to walk through the specified set of orphaned elements, check whether they have expired, and evict them from the store.
 V get(Object key)
           
 CacheConfig getConfig()
          Get the current config for the cache.
 EvictionStatistics getStatistics()
          Retrieve the statistics for the current JVM.
 TimeSource getTimeSource()
          Return the time source used by this cache to generate entry timestamps.
 TimestampedValue<V> getTimestampedValue(Object key)
          Get the value wrapped in a timestamped object.
 TimestampedValue<V> getTimestampedValueQuiet(Object key)
          Get the value wrapped in a timestamped object without updating the last usage statistics.
 boolean isEmpty()
           
protected  boolean isEvictionEnabled()
           
 boolean isStatisticsEnabled()
          Returns whether statistics are enabled.
 Set<K> keySet()
           
 int localSize()
          Get the number of elements local in the current node.
 void lockEntry(K key)
          Lock the entry corresponding to this key.
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> t)
           
 V putIfAbsent(K key, V value)
           
 void putNoReturn(K key, V value)
          Put the key/value into the map, replacing the existing value if present.
 V remove(Object key)
           
 boolean remove(Object key, Object value)
           
 void removeNoReturn(Object key)
          Remove the key from the map, if it exists.
 TimestampedValue<V> removeTimestampedValue(K key)
          Remove the timestamped value for the given key.
 V replace(K key, V value)
           
 boolean replace(K key, V oldValue, V newValue)
           
 void setStatisticsEnabled(boolean enabled)
          Toggle statistics being enabled or not.
 void setTimeSource(TimeSource timeSource)
          This is provided for testing purposes - it lets you override the source of System.currentTimeMillis() so that you can control it yourself in the test.
 void shutdown()
          Shut down the background eviction thread, if any.
 int size()
           
 void unlockEntry(K key)
          Unlock the entry corresponding to this key
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

data

protected final ConcurrentDistributedMap<K,TimestampedValue<V>> data
Constructor Detail

LocalCache

public LocalCache(CacheConfig config)
Construct a cache with the given configuration

Parameters:
config - cache configuration
Method Detail

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>

getTimestampedValue

public TimestampedValue<V> getTimestampedValue(Object key)
Description copied from interface: DistributedCache
Get the value wrapped in a timestamped object.

This value is used entirely for maintaining the creation and last accessed time for TTI and TTL eviction.

Specified by:
getTimestampedValue in interface DistributedCache<K,V>
Parameters:
key - The key to search for
Returns:
The wrapped value or null if not found

getTimestampedValueQuiet

public TimestampedValue<V> getTimestampedValueQuiet(Object key)
Description copied from interface: DistributedCache
Get the value wrapped in a timestamped object without updating the last usage statistics.

Specified by:
getTimestampedValueQuiet in interface DistributedCache<K,V>
Parameters:
key - The key to search for
Returns:
The wrapped value or null if not found
See Also:
DistributedCache.getTimestampedValue(Object)

removeTimestampedValue

public TimestampedValue<V> removeTimestampedValue(K key)
Description copied from interface: DistributedCache
Remove the timestamped value for the given key.

Previous values will be returned as null if they are expired.

Specified by:
removeTimestampedValue in interface DistributedCache<K,V>
Parameters:
key - The key to search for
Returns:
The wrapped value or null if not found or expired

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>

localSize

public int localSize()
Description copied from interface: DistributedCache
Get the number of elements local in the current node.

This number should always be less than size(), and is likely to be different as all cache entries are not required to be stored in the local heap.

Specified by:
localSize in interface DistributedCache<K,V>
Returns:
The local size, in range, such that 0 < localSize < size

shutdown

public void shutdown()
Description copied from interface: DistributedCache
Shut down the background eviction thread, if any.

Specified by:
shutdown in interface DistributedCache<K,V>

evictExpiredLocalElements

public void evictExpiredLocalElements()
Description copied from interface: Evictable
Trigger to walk through the local elements, look for expired elements, and evict them.

Specified by:
evictExpiredLocalElements in interface Evictable<K>

evictOrphanElements

public void evictOrphanElements(ClusterInfo cluster)
Description copied from interface: Evictable
Trigger to walk through the specified set of orphaned elements, check whether they have expired, and evict them from the store.

Specified by:
evictOrphanElements in interface Evictable<K>
Parameters:
cluster - The Terracotta cluster info which can be used to obtain the orphaned element list

setTimeSource

public void setTimeSource(TimeSource timeSource)
This is provided for testing purposes - it lets you override the source of System.currentTimeMillis() so that you can control it yourself in the test. If it's not called, SystemTimeSource is used which just calls System.currentTimeMillis().

Parameters:
timeSource - The alternate TimeSource implementation

getTimeSource

public TimeSource getTimeSource()
Return the time source used by this cache to generate entry timestamps.

Returns:
this cache's timesource

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

putNoReturn

public void putNoReturn(K key,
                        V value)
Description copied from interface: DistributedCache
Put the key/value into the map, replacing the existing value if present.

This method differs from the normal put() method only in that it returns void instead of the prior value. This is important in the cluster as it avoids faulting that value to return when it is rarely used.

Specified by:
putNoReturn in interface DistributedCache<K,V>
Parameters:
key - Key
value - Value

removeNoReturn

public void removeNoReturn(Object key)
Description copied from interface: DistributedCache
Remove the key from the map, if it exists.

This method differs from the normal remove() method only in that it returns void instead of the prior value. This is important in the cluster as it avoids faulting that value to return it when it is rarely used.

Specified by:
removeNoReturn in interface DistributedCache<K,V>
Parameters:
key - Key

putIfAbsent

public V putIfAbsent(K key,
                     V value)
Specified by:
putIfAbsent in interface ConcurrentMap<K,V>

replace

public V replace(K key,
                 V value)
Specified by:
replace in interface ConcurrentMap<K,V>

isStatisticsEnabled

public boolean isStatisticsEnabled()
Description copied from interface: Evictable
Returns whether statistics are enabled.

Specified by:
isStatisticsEnabled in interface Evictable<K>
Returns:
true when statistics are enabled; and false otherwise

setStatisticsEnabled

public void setStatisticsEnabled(boolean enabled)
Description copied from interface: Evictable
Toggle statistics being enabled or not. When statistics are disabled, the already accumulated data will be reset.

Specified by:
setStatisticsEnabled in interface Evictable<K>
Parameters:
enabled - true to enable statistics; and false to disable them

getStatistics

public EvictionStatistics getStatistics()
Description copied from interface: Evictable
Retrieve the statistics for the current JVM.

Specified by:
getStatistics in interface Evictable<K>
Returns:
the statistics instance for the current JVM, statistics aren't shared across the cluster

getConfig

public CacheConfig getConfig()
Description copied from interface: DistributedCache
Get the current config for the cache.

This may or may not be the same config object as was orignally supplied to the cache.

Specified by:
getConfig in interface DistributedCache<K,V>
Returns:
The configuration for this cache

isEvictionEnabled

protected boolean isEvictionEnabled()

remove

public boolean remove(Object key,
                      Object value)
Specified by:
remove in interface ConcurrentMap<K,V>

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)
Specified by:
replace in interface ConcurrentMap<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> t)
Specified by:
putAll in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

createFinegrainedLock

public ClusteredLock createFinegrainedLock(K key)
Description copied from interface: LockableMap
Obtain a fine-grained lock instance for a particular key.

Specified by:
createFinegrainedLock in interface LockableMap<K>
Parameters:
key - the key for which a fine-grained lock has to be constructed
Returns:
an instance of fine-grained lock that can be used to perform primitive locking operations on a key

lockEntry

public void lockEntry(K key)
Description copied from interface: LockableMap
Lock the entry corresponding to this key.

Specified by:
lockEntry in interface LockableMap<K>
Parameters:
key - the key for which all entry accesses have to be locked

unlockEntry

public void unlockEntry(K key)
Description copied from interface: LockableMap
Unlock the entry corresponding to this key

Specified by:
unlockEntry in interface LockableMap<K>
Parameters:
key - the key for which all entry accesses have to be unlocked


Copyright © 2015 Terracotta, Inc.. All Rights Reserved.