24/68

# 15 Cache Configurations by Example

This section provides a series of basic cache scheme definitions that can be used or modified as required. See Chapter 11, "Configuring Caches," for detailed instructions on how to configure caches. In addition, the samples in this chapter build upon one another and often use a `<scheme-ref>` element to reuse other samples as nested schemes. See "Using Scheme Inheritance" for details on using the `<scheme-ref>` element. Lastly, these samples only specify a minimum number of settings, follow the embedded links to a scheme's documentation to see the full set of options.

This section describes configurations for the following caching scenarios:

- Local Caches (accessible from a single JVM)

  - In-memory Cache

  - NIO In-memory Cache

  - Size Limited In-memory Cache

  - In-memory Cache with Expiring Entries

  - Cache on Disk

  - Size Limited Cache on Disk

  - Persistent Cache on Disk

  - In-memory Cache with Disk Based Overflow

  - Cache of a Database

- Clustered Caches (accessible from multiple JVMs)

  - Replicated Cache

  - Replicated Cache with Overflow

  - Partitioned Cache

  - Partitioned Cache with Overflow

  - Partitioned Cache of a Database

  - Partitioned Cache with a Serializer

  - Local Cache of a Partitioned Cache (Near cache)

## Local Caches (accessible from a single JVM)

This section defines a series of local cache schemes. In this context "local" means that the cache is only directly accessible by a single JVM. Later in this document local caches will be used as building blocks for clustered caches. See "Clustered Caches (accessible from multiple JVMs)".

### In-memory Cache

Example 15-1 uses a `local-scheme` to define an in-memory cache. The cache will store as much as the JVM heap will allow.

Example 15-1 Configuration for a Local, In-memory Cache

    <local-scheme>
       <scheme-name>SampleMemoryScheme</scheme-name>
    </local-scheme>

### NIO In-memory Cache

Example 15-2 uses an `external-scheme` to define an in-memory cache using an `nio-memory-manager`. The advantage of an NIO memory based cache is that it allows for large in-memory cache storage while not negatively impacting the JVM's GC times. The size of the cache is limited by the maximum size of the NIO memory region. See the `<maximum-size>` subelement of `nio-memory-manager`.

Example 15-2 Configuration for a NIO In-memory Cache

    <external-scheme>
      <scheme-name>SampleNioMemoryScheme</scheme-name>
      <nio-memory-manager/>
    </external-scheme>

### Size Limited In-memory Cache

Adding a `<high-units>` sub element to <`local-scheme>` limits the size of the cache. Here the cache is size limited to one thousand entries. When the limit is exceeded, the scheme's `<eviction-policy>` will determine which elements to evict from the cache.

Example 15-3 Configuration for a Size Limited, In-memory, Local Cache

    <local-scheme>
       <scheme-name>SampleMemoryLimitedScheme</scheme-name>
       <high-units>1000</high-units>
    </local-scheme>

### In-memory Cache with Expiring Entries

Adding an `<expiry-delay>` subelement to <`local-scheme`> will cause cache entries to automatically expire if they are not updated for a given time interval. When expired the cache will invalidate the entry, and remove it from the cache.

Example 15-4 Configuration for an In-memory Cache with Expiring Entries

    <local-scheme>
       <scheme-name>SampleMemoryExpirationScheme</scheme-name>
       <expiry-delay>5m</expiry-delay>
    </local-scheme>

### Cache on Disk

Example 15-5 uses an `external-scheme` to define an on disk cache. The cache will store as much as the file system will allow.

Note:

This example uses the `lh-file-manager` for its on disk storage implementation. See external-scheme for additional external storage options.

Example 15-5 Configuration to Define a Cache on Disk

    <external-scheme>
      <scheme-name>SampleDiskScheme</scheme-name>
      <lh-file-manager/>
    </external-scheme>

### Size Limited Cache on Disk

Adding a `<high-units>` sub- element to `external-scheme` limits the size of the cache. The cache is size limited to one million entries. When the limit is exceeded, LRU eviction is used determine which elements to evict from the cache. Refer to "paged-external-scheme" for an alternate size limited external caching approach.

Example 15-6 Configuration for a Size Limited Cache on Disk

    <external-scheme>
      <scheme-name>SampleDiskLimitedScheme</scheme-name>
      <lh-file-manager/>
      <high-units>1000000</high-units>
    </external-scheme>

### Persistent Cache on Disk

Example 15-7 uses an `external-scheme` to implement a cache suitable for use as long-term storage for a single JVM.

External caches are generally used for temporary storage of large data sets, and are automatically deleted on JVM shutdown. An external-cache can be used for long term storage (see "Persistence (long-term storage)") in non-clustered caches when using either the `lh-file-manager` or `bdb-store-manager` storage managers. For clustered persistence see the "Partitioned Cache of a Database" sample.

The `{cache-name}` macro is used to specify the name of the file the data will be stored in. See "Using Parameter Macros" for more information on this macro.

Example 15-7 Configuration for Persistent cache on disk

    <external-scheme>
      <scheme-name>SampleDiskPersistentScheme</scheme-name>
      <lh-file-manager>
        <directory>/my/storage/directory</directory>
        <file-name>{cache-name}.store</file-name>
      </lh-file-manager>
    </external-scheme>

Example 15-8 illustrates using Berkeley DB rather then LH.

Example 15-8 Configuration for Persistent cache on disk with Berkeley DB

    <external-scheme>
      <scheme-name>SampleDiskPersistentScheme</scheme-name>
      <bdb-store-manager>
        <directory>/my/storage/directory</directory>
        <store-name>{cache-name}.store</store-name>
      </bdb-store-manager>
    </external-scheme>

### In-memory Cache with Disk Based Overflow

Example 15-9 uses an `overflow-scheme` to define a size limited in-memory cache, when the in-memory (`<front-scheme>`) size limit is reached, a portion of the cache contents will be moved to the on disk (`<back-scheme>`). The front-scheme's `<eviction-policy>` will determine which elements to move from the front to the back.

Note that this example reuses the examples in "Size Limited Cache on Disk" and "Cache on Disk". to implement the front and back of the cache.

Example 15-9 Configuration for In-memory Cache with Disk Based Overflow

    <overflow-scheme>
      <scheme-name>SampleOverflowScheme</scheme-name>
      <front-scheme>
        <local-scheme>
          <scheme-ref>SampleMemoryLimitedScheme</scheme-ref>
        </local-scheme>
      </front-scheme>
      <back-scheme>
        <external-scheme>
          <scheme-ref>SampleDiskScheme</scheme-ref>
        </external-scheme>
      </back-scheme>
    </overflow-scheme>

### Cache of a Database

Example 15-10 uses a `read-write-backing-map-scheme` to define a cache of a database. This scheme maintains local cache of a portion of the database contents. Cache misses will read-through to the database, and cache writes will be written back to the database.

The `cachestore-scheme` element is configured with a custom class implementing either the `com.tangosol.net.cache.CacheLoader` or `com.tangosol.net.cache.CacheStore` interface. This class is responsible for all operations against the database, such as reading and writing cache entries. See "Sample CacheStore" implementations for examples of writing a cache store.

The `{cache-name}` macro is used to inform the cache store implementation of the name of the cache it will back. See "Using Parameter Macros" for more information on this macro.

Example 15-10 Configuration for the Cache of a Database

    <read-write-backing-map-scheme>
      <scheme-name>SampleDatabaseScheme</scheme-name>
      <internal-cache-scheme>
        <local-scheme>
          <scheme-ref>SampleMemoryScheme</scheme-ref>
        </local-scheme>
      </internal-cache-scheme>
      <cachestore-scheme>
        <class-scheme>
          <class-name>com.tangosol.examples.coherence.DBCacheStore</class-name>
          <init-params>
            <init-param>
              <param-type>java.lang.String</param-type>
              <param-value>{cache-name}</param-value>
            </init-param>
          </init-params>
        </class-scheme>
      </cachestore-scheme>
    </read-write-backing-map-scheme>

## Clustered Caches (accessible from multiple JVMs)

This section defines a series of clustered cache examples. Clustered caches are accessible from multiple JVMs (any cluster node running the same cache service). The internal cache storage (backing-map) on each cluster node is defined using local caches (see "Local Caches (accessible from a single JVM)"). The cache service provides the capability to access local caches from other cluster nodes.

### Replicated Cache

Example 15-11 uses the `replicated-scheme` element to define a clustered cache in which a copy of each cache entry will be stored on all cluster nodes.

The sample in "In-memory Cache" is used to define the cache storage on each cluster node. The size of the cache is only limited by the cluster node with the smallest JVM heap.

Example 15-11 Configuration for a Replicated Cache

    <replicated-scheme>
      <scheme-name>SampleReplicatedScheme</scheme-name>
      <backing-map-scheme>
        <local-scheme>
          <scheme-ref>SampleMemoryScheme</scheme-ref>
        </local-scheme>
      </backing-map-scheme>
    </replicated-scheme>

### Replicated Cache with Overflow

The `backing-map-scheme` element could just as easily specify any of the other local cache samples. For instance, if it had used the "In-memory Cache with Disk Based Overflow", each cluster node would have a local overflow cache allowing for much greater storage capacity.

Example 15-12 Configuration for a Replicated Cache with Overflow

    <replicated-scheme>
      <scheme-name>SampleReplicatedOverflowScheme</scheme-name>
      <backing-map-scheme>
        <overflow-scheme>
          <scheme-ref>SampleOverflowScheme</scheme-ref>
        </overflow-scheme>
      </backing-map-scheme>
    </replicated-scheme>

### Partitioned Cache

Example 15-13 uses the `distributed-scheme` to define a clustered cache in which cache storage is partitioned across all cluster nodes.

The "In-memory Cache" is used to define the cache storage on each cluster node. The total storage capacity of the cache is the sum of all storage enabled cluster nodes running the partitioned cache service. See the `<local-storage>` subelement of "distributed-scheme".

Example 15-13 Configuration for a Partitioned Cache

    <distributed-scheme>
      <scheme-name>SamplePartitionedScheme</scheme-name>
      <backing-map-scheme>
        <local-scheme>
          <scheme-ref>SampleMemoryScheme</scheme-ref>
        </local-scheme>
      </backing-map-scheme>
    </distributed-scheme>

### Partitioned Cache with Overflow

The `backing-map-scheme` element could just as easily specify any of the other local cache samples. For instance if it had used the "In-memory Cache with Disk Based Overflow", each storage-enabled cluster node would have a local overflow cache allowing for much greater storage capacity. Note that the cache's backup storage also uses the same overflow scheme which allows for backup data to be overflowed to disk.

Example 15-14 Configuration for a Partitioned Cache with Overflow

    <distributed-scheme>
      <scheme-name>SamplePartitionedOverflowScheme</scheme-name>
      <backing-map-scheme>
        <overflow-scheme>
          <scheme-ref>SampleOverflowScheme</scheme-ref>
        </overflow-scheme>
      </backing-map-scheme>
      <backup-storage>
        <type>scheme</type>
        <scheme-name>SampleOverflowScheme</scheme-name>
      </backup-storage>
    </distributed-scheme>

### Partitioned Cache of a Database

Switching the `backing-map-scheme` element to use a `read-write-backing-map-scheme` allows the cache to load and store entries against an external source such as a database.

Example 15-15 reuses the "Cache of a Database" to define the database access.

Example 15-15 Configuration for a Partitioned Cache of a Database

    <distributed-scheme>
      <scheme-name>SamplePartitionedDatabaseScheme</scheme-name>
      <backing-map-scheme>
        <read-write-backing-map-scheme>
          <scheme-ref>SampleDatabaseScheme</scheme-ref>
        </read-write-backing-map-scheme>
      </backing-map-scheme>
    </distributed-scheme>

### Partitioned Cache with a Serializer

Example 15-16 uses the serializer element in `distributed-scheme` to define a serializer that will be used to serialize and deserialize user types. In this case, the partitioned cache will use POF (`ConfigurablePofContext`) as its serialization format. Note that if you use POF and your application uses any custom user type classes, then you must also define a custom POF configuration for them. See Appendix D, "POF User Type Configuration Elements" for more information on POF elements.

Example 15-16 Configuration for a Partitioned Cache with a Serializer

    <distributed-scheme>
      <scheme-name>SamplePartitionedPofScheme</scheme-name>
      <service-name>PartitionedPofCache</service-name>
      <serializer>
        <class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
      </serializer>
      <backing-map-scheme>
        <local-scheme/>
      </backing-map-scheme>
      <autostart>true</autostart>
    </distributed-scheme>

### Local Cache of a Partitioned Cache (Near cache)

Example 15-17 uses the `near-scheme` to define a local in-memory cache of a subset of a partitioned cache. The result is that any cluster node accessing the partitioned cache will maintain a local copy of the elements it frequently accesses. This offers read performance close to the `replicated-scheme`-based caches, while offering the high scalability of a distributed-scheme-based cache.

The "Size Limited In-memory Cache" sample is reused to define the "near" (`<front-scheme>`) cache, while the "Partitioned Cache" sample is reused to define the `near-scheme`.

Note that the size limited configuration of the front-scheme specifies the limit on how much of the back-scheme cache is locally cached.

Example 15-17 Configuration for a Local Cache of a Partitioned Cache

    <near-scheme>
      <scheme-name>SampleNearScheme</scheme-name>
      <front-scheme>
        <local-scheme>
           <scheme-ref>SampleLimitedMemoryScheme</scheme-ref>
        </local-scheme>
      </front-scheme>
      <back-scheme>
        <distributed-scheme>
          <scheme-ref>SamplePartitionedScheme</scheme-ref>
        </distributed-scheme>
      </back-scheme>
    </near-scheme>