{div:style=margin-top:20px}
{include:documentation-index}
{div}
{div9:id=documentationcolumn}
{include:HeaderBasic}

h1. Terracotta Cache Evictor
{toc:minLevel=2|maxLevel=2}

h2. Introduction
The Terracotta Cache Evictor is an interface providing a simple distributed eviction solution for map elements. The Cache Evictor, implemented with the Terracotta Integration Module [{{tim-map-evictor}}|http://forge.terracotta.org/releases/projects.html#tim-map-evictor], provides a number of advantages over more complex solutions:

* Simple -- API is easy to understand and code against.
* Distributed -- Eviction is distributed along with data to maintain coherence.
* Standard -- Data eviction is based on standard expiration metrics.
* Lightweight -- Implementation does not hog resources.
* Efficient -- Optimized for a clustered environment to minimize faulting due to low locality of reference.
* Fail-safe -- Data can be evicted even if written by a failed node or after all nodes have been restarted. 
* Native -- Designed for Terracotta to eliminate integration issues.

h2. How to Implement and Configure
Under the appropriate conditions, the Terracotta Cache Evictor can be used in any Terracotta cluster. It can be implemented by any data structure that meets 

* Use a partial-loading data structure (see [Clustered Data Structures Guide]).
* Write start/stop thread-management code to run the evictor.
* Include the code from CHMDistributedMap that performs local eviction (see the {{tim-map-evictor}} library).

h3. Characteristics and Requirements
The Terracotta Cache Evictor has the following eviction characteristics:

* A Time To Live (TTL) value can be set, but only for all elements in a data structure. 
The TTL determines the maximum amount of time an object can remain in the cache before becoming eligible for eviction, regardless of other conditions such as use.
* A Time To Idle (TTI) value can be set, but only for all elements in a data structure.
The TTI determines the maximum amount of time an object can remain idle in the cache before becoming eligible for eviction. TTI is reset each time the object is used.
* Each element does receive its own timestamp used against the cache-wide TTL and TTI.
* "Orphaned" values (values no longer local to any node) are evicted in batches.

The Terracotta Cache Evictor requires JDK 1.5 or greater.

h3. Implementing the Evictor Interface
partial data structures 





h2. A Simple Cache with Terracotta Cache Evictor
Clustered applications with a system of record (SOR) on the backend can benefit from a distributed cache that manages certain data in memory while reducing costly application-SOR interactions. However, using a cache can introduce increased complexity to software development, integration, operation, and maintenance.

The Terracotta Cache Evictor includes a distributed-map that can be used as a simple distributed cache. This cache uses the Terracotta Cache Evictor, incorporating all of its benefits. It also takes both established and innovative approaches to the caching model, solving performance and complexity issues by:

* obviate SOR commits for data with a limited lifetime;
* making cached application data available in-memory across a cluster of application servers;
* offering standard methods for working with cache elements and performing cache-wide operations;
* incorporating concurrency for readers and writers;
* utilizing a flexible map implementation to adapt to more applications;
* minimizing inter-node faulting to speed data operations.

h3. Structure and Characteristics
The Terracotta distributed cache is an interface incorporating a distributed map with standard map operations:

{code}
public interface DistributedMap<K, V> {
  // Single item operations
  void put(K key, V value);
  V get(K key);
  V remove(K key);
  boolean containsKey(K key);

  // Multi item operations
  int size();
  void clear();
  Set<K> getKeys();

  // For managing the background evictor thread
  void start();
  void shutdown();
}
{code}

{tip}
getValues() is not provided, but an iterator can be obtained for Set<K> to obtain values.
{tip}

{comment}
* The map containing the timestamps is clustered, allowing all nodes to be aware of expirations and pending evictions.
{comment}

h3. Usage Pattern
A typical usage pattern for the Terracotta Cache Evictor is shown in the MyStuff class below. The next section contains a full list of configuration parameters available to {{DistributedMapBuilder}}.

{code:java}
import org.terracotta.modules.dmap.DistributedMap;
import org.terracotta.modules.dmap.DistributedMapBuilder;
import static org.terracotta.modules.dmap.DistributedMapBuilder.HOUR;
import static org.terracotta.modules.dmap.DistributedMapBuilder.MINUTE;

public class MyStuff {

   // Mark as Terracotta root
   private DistributedMap<String, Stuff> sharedMap;

   public MyStuff() {
     if(sharedMap == null) {
       DistributedMap<String,Stuff> newMap = new DistributedMapBuilder() 
            .setMaxTTLMillis(6*HOUR)                                            // regardless of use, remove after 6 hours
           .setMaxTTIMillis(30*MINUTE)                                  // remove after 30 minutes of unuse
           .setEvictorSleepMillis(5*MINUTE)                             // only do eviction every 5 minutes
           .newMap();

       // set root - if this doesn't succeed, shutdown the newMap as it has a worthless background evictor thread
       sharedMap = newMap;
       if(sharedMap != newMap) {
         newMap.shutdown();
       }
   }

   public void putStuff(String key, Stuff stuff) {
       sharedMap.put(key, stuff);
   }

   public Stuff getStuff(String key) {
       return sharedMap.get(key);
   }
}
{code}

h4. Evictor Configuration Parameters

||Config property || Default value || Description ||
|name | "Distributed Map" | A descriptive string used in log messages and evictor thread names. |
|concurrency | 16 | The concurrency, or maximum number of accessors allowed at one time, in the map implementation. |
|maxTTIMillis | 0 | Time To Idle - the maximum amount of time (in milliseconds) an item can be in the map unused before expiration; 0 means never expire due to TTI. |
|maxTTLMillis | 0 | Time To Live - the maximum amount of time (in milliseconds) an item may be in the map regardless of use before expiration; 0 means never expire due to TTL. |
|evictorSleepMillis | 30000 | Wait time (in milliseconds) between eviction cycles; should be tuned to work well with TTI/TTL values. |
|orphanEvictionEnabled | true | Determines whether "orphaned" values (values no longer local to any node) are evicted. |
|orphanEvictionFrequency | 4 | Number of times to run local eviction between doing orphan eviction. |
|orphanBatchSize | 1000 | Size of each set of items evicted during orphan eviction. |
|orphanBatchPauseMillis | 20 | Rest time between each orphan batch eviction. |
|loggingEnabled | false | Basic distributed map logging. |
|evictorLoggingEnabled | false | Eviction logging. |

h3. Usage Example

The following is an example of a cache that implements the Terracotta distributed cache:

{code}
import org.terracotta.modules.dmap.*;
import static org.terracotta.modules.dmap.DistributedMapBuilder.*;


DisributedMap<String,String> map = new DistributedMapBuilder()
.setMaxTTLMillis(10 * SECOND)
.setMaxTTIMillis(5 * SECOND)
.setConcurrency(16)
.newMap();
map.start(); // start eviction thread


map.put("Rabbit", "Carrots");
map.put("Dog", "Bone");
map.put("Owl", "Mouse");
// wait 3 seconds
map.get("Rabbit");


// wait 2 seconds - expire Dog and Owl due to TTI
assert ! map.containsKey("Dog");
assert ! map.containsKey("Owl");
assert map.containsKey("Rabbit");


// wait 5 seconds - expire Rabbit due to TTL
assert ! map.containsKey("Rabbit");
{code}

h2. Terracotta Cache Evictor in a Reference Application
The [Examinator reference application|orgsite:Web App Reference Implementation] uses the Terracotta Cache Evictor to handle pending user registrations. This type of data has a "medium-term" lifetime which needs to be persisted long enough to give prospective registrants a chance to verify their registrations. If a registration isn't verified by the time TTL is reached, it can be evicted from the cache. Only if the registration is verified is it written to the database. 

The combination of Terracotta and the Terracotta Cache Evictor gives Examinator the following advantages:

* The simple Terracotta Cache Evictor's API makes it easy to integrate with Examinator and to maintain and troubleshoot.
* Medium-term data is not written to the database unnecessarily, improving application performance.
* Terracotta persists the pending registrations so they can survive node failure.
* Terracotta clusters (shares) the pending registration data so that any node can handle validation.

{div9}