17 Cache Configurations by Example
<scheme-ref>
element to reuse other samples as nested schemes. See Using Scheme Inheritance. Lastly, the 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 chapter contains the following sections:
- Local Caches (accessible from a single JVM)
Use the local cache scheme samples to learn how to configure caches that are accessible by a single JVM. - Clustered Caches (accessible from multiple JVMs)
Use the clustered cache scheme samples to learn how to configure clustered caches that are accessible from multiple JVMs (any cluster node running the same cache service).
Parent topic: Using Caches
Local Caches (accessible from a single JVM)
This section contains the following topics:
- In-memory Cache
- Size Limited In-memory Cache
- In-memory Cache with Expiring Entries
- In-memory Cache with Disk Based Overflow
- Cache on Disk
- Size Limited Cache on Disk
- Persistent Cache on Disk
- Cache of a Database
Parent topic: Cache Configurations by Example
In-memory Cache
Example 17-1 uses a local-scheme
element to define an in-memory cache. The cache stores as much as the JVM heap allows.
Example 17-1 Configuration for a Local, In-memory Cache
<local-scheme> <scheme-name>SampleMemoryScheme</scheme-name> </local-scheme>
Parent topic: Local Caches (accessible from a single JVM)
Size Limited In-memory Cache
Adding a <high-units>
subelement within the <local-scheme>
element 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>
determines which elements to evict from the cache. See local-scheme.
Example 17-2 Configuration for a Size Limited, In-memory, Local Cache
<local-scheme> <scheme-name>SampleMemoryLimitedScheme</scheme-name> <high-units>1000</high-units> </local-scheme>
Parent topic: Local Caches (accessible from a single JVM)
In-memory Cache with Expiring Entries
Adding an <expiry-delay>
subelement within the <local-scheme>
element causes cache entries to automatically expire if they are not updated for a given time interval. When expired the cache invalidates the entry, and remove it from the cache. See local-scheme.
Example 17-3 Configuration for an In-memory Cache with Expiring Entries
<local-scheme> <scheme-name>SampleMemoryExpirationScheme</scheme-name> <expiry-delay>5m</expiry-delay> </local-scheme>
Parent topic: Local Caches (accessible from a single JVM)
In-memory Cache with Disk Based Overflow
Example 17-4 uses an overflow-scheme
element to define a size limited in-memory cache, when the in-memory (<front-scheme>
) size limit is reached, a portion of the cache contents are moved to the on disk (<back-scheme>
). The front-scheme's <eviction-policy>
determines 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 17-4 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>
Parent topic: Local Caches (accessible from a single JVM)
Cache on Disk
Example 17-5 uses an external-scheme
element to define an on-disk cache.
Note:
This example uses the bdb-store-manager
(Berkeley Database) for its on disk storage implementation. See external-scheme for additional external storage options.
Example 17-5 Configuration to Define a Cache on Disk
<external-scheme> <scheme-name>SampleDiskScheme</scheme-name> <bdb-store-manager/> </external-scheme>
Parent topic: Local Caches (accessible from a single JVM)
Size Limited Cache on Disk
Adding a <high-units>
subelement to an external-scheme
element 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. See paged-external-scheme for an alternate size-limited external caching approach.
Example 17-6 Configuration for a Size Limited Cache on Disk
<external-scheme> <scheme-name>SampleDiskLimitedScheme</scheme-name> <bdb-store-manager/> <high-units>1000000</high-units> </external-scheme>
Parent topic: Local Caches (accessible from a single JVM)
Persistent Cache on Disk
Example 17-7 uses an external-scheme
element to define 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 in non-clustered caches when using either of the bdb-store-manager
storage managers. See Persistence (long-term storage). For clustered persistence, see Partitioned Cache of a Database.
The {cache-name}
macro is used to specify the name of the file the data is stored in. See Using Parameter Macros.
Example 17-7 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>
Parent topic: Local Caches (accessible from a single JVM)
Cache of a Database
Example 17-8 uses a read-write-backing-map-scheme
element to define a cache of a database. This scheme maintains local cache of a portion of the database contents. Cache misses are read-through to the database, and cache writes are 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 Cache Store Implementation.
The {cache-name}
macro is used to inform the cache store implementation of the name of the cache it backs. See Using Parameter Macros.
Example 17-8 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>
Parent topic: Local Caches (accessible from a single JVM)
Clustered Caches (accessible from multiple JVMs)
This section contains the following topics:
- Partitioned Cache
- Partitioned Cache with Overflow
- Partitioned Cache with Journal Storage
- Partitioned Cache of a Database
- Partitioned Cache with a Serializer
- Near Cache
- View Cache
- Replicated Cache
- Replicated Cache with Overflow
Parent topic: Cache Configurations by Example
Partitioned Cache
Example 17-9 uses the distributed-scheme
element to define a clustered cache in which cache storage is partitioned across all cluster nodes.
An 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.
Example 17-9 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>
Parent topic: Clustered Caches (accessible from multiple JVMs)
Partitioned Cache with Overflow
The backing-map-scheme
element can use a scheme reference to 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 17-10 Configuration for a Partitioned Cache with Overflow
<distributed-scheme>
<scheme-name>SamplePartitionedOverflowScheme</scheme-name>
<backup-storage>
<type>scheme</type>
<scheme-name>SampleOverflowScheme</scheme-name>
</backup-storage>
<backing-map-scheme>
<overflow-scheme>
<scheme-ref>SampleOverflowScheme</scheme-ref>
</overflow-scheme>
</backing-map-scheme>
</distributed-scheme>
Parent topic: Clustered Caches (accessible from multiple JVMs)
Partitioned Cache with Journal Storage
Example 17-11 uses the backing-map-scheme
element to define a partitioned cache that uses a RAM journal for the backing map. The RAM journal automatically delegates to a flash journal when the RAM journal exceeds a configured memory size. See Defining Journal Schemes.
Example 17-11 Configuration for a Partitioned Cache with RAM Journaling
<distributed-scheme> <scheme-name>SamplePartitionedJournalScheme</scheme-name> <backing-map-scheme> <ramjournal-scheme/> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme>
Example 17-12 defines a partitioned cache that directly uses a flash journal for the backing map.
Example 17-12 Configuration for a Partitioned Cache with Flash Journaling
<distributed-scheme> <scheme-name>SamplePartitionedJournalScheme</scheme-name> <backing-map-scheme> <flashjournal-scheme/> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme>
Parent topic: Clustered Caches (accessible from multiple JVMs)
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 17-13 reuses Cache of a Database to define database access.
Example 17-13 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> <internal-cache-scheme
> <local-scheme> <scheme-ref>SampleMemoryScheme</scheme-ref> </local-scheme> </internal-cache-scheme
> </read-write-backing-map-scheme> </backing-map-scheme> </distributed-scheme>
Parent topic: Clustered Caches (accessible from multiple JVMs)
Partitioned Cache with a Serializer
Example 17-14 uses the serializer
element to define a serializer that is used to serialize and deserialize user types. In this case, the partitioned cache uses 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 POF User Type Configuration Elements.
Example 17-14 Configuration for a Partitioned Cache with a Serializer
<distributed-scheme> <scheme-name>SamplePartitionedPofScheme</scheme-name> <service-name>PartitionedPofCache</service-name> <serializer> <instance> <class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name> </instance> </serializer> <backing-map-scheme> <local-scheme/> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme>
Serializers that are defined in the tangosol-coherence.xml
deployment descriptor can also be referenced.
Example 17-15 Partitioned Cache that References a Serializer
<distributed-scheme> <scheme-name>SamplePartitionedPofScheme</scheme-name> <service-name>PartitionedPofCache</service-name> <serializer>pof</serializer> <backing-map-scheme> <local-scheme/> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme>
Lastly a default serializer can be defined for all cache schemes and alleviates having to explicitly include a <serializer>
element in each cache scheme definition. The global serializer definitions can also reference serializers that are defined in the tangosol-coherence.xml
deployment descriptor
Example 17-16 Defining a Default Serializer
<?xml version='1.0'?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <defaults> <serializer>pof</serializer> </defaults> ...
Parent topic: Clustered Caches (accessible from multiple JVMs)
Near Cache
Example 17-17 uses the near-scheme
element 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 maintains a local copy of the elements it frequently accesses. This offers read performance close to a replicated-scheme
caches, while offering the high scalability of a distributed-scheme
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 back (<back-scheme>
) cache.
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 17-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>
Parent topic: Clustered Caches (accessible from multiple JVMs)
View Cache
A view cache is configured by using the view-scheme
element in the coherence-cache-config file. The
<view-scheme>
element has only one required sub-element,
which is the name of the scheme. If no other configuration elements are applied, the
result lists a local view of all the entries of an existing cache. By default, all
entries of the back cache will be visible within the view. However, it is possible to
restrict the result set by providing a Coherence filter through the <filter>
element.
Entries can have transformations applied prior to insertion into the view,
by specifying a custom ValueExtractor
with the
<view-scheme>
element. Applying transformation marks view as
read-only
and thus disallows mutations. Additionally, if the view
is being transformed and values are not being stored then operations such as
aggregations will not be possible and result in a runtime error.
A MapListener
could be declared to allow interception of all
map events including those generated while materializing the view.
The <read-only>
element disables all mutations of the
back cache through the view. However, modifications could still occur on the back cache
directly.
The <reconnect-interval>
specified in milliseconds,
indicates the period in which re-synchronization with the underlying cache will be
delayed in the case the connection is severed. During this time period, local content
can be accessed without triggering re-synchronization of the local content. A value of
zero means that the view cannot be used when not connected.
The <back-scheme>
element refers to a distributed or
remote scheme (if using Coherence*Extend).
Example 17-18 Configuration for a View Cache
<view-scheme> <scheme-name>view</scheme-name> </view-scheme>
This example is a minimum configuration of a view cache. A view will be materialized on the default view cache service with no filtering, listeners, or transformation of entries. It is a complete local copy of an existing cache.
Example 17-19 Configuration for a View With a Custom Filter
<view-scheme> <scheme-name>view-customers-over-40</scheme-name> <view-filter> <class-scheme> <class-name>com.tangosol.util.filter.GreaterFilter</class-name> <init-params> <init-param> <param-type>java.lang.String</param-type> <param-value>age</param-value> </init-param> <init-param> <param-type>java.lang.Integer</param-type> <param-value>40</param-value> </init-param> </init-params> </class-scheme> </view-filter> <back-scheme> <distributed-scheme> <scheme-ref>partitioned-std</scheme-ref> </distributed-scheme> </back-scheme> </view-scheme>
In this example, the view uses partitioned cache from the
partitioned-std
cache service and only include entries of
customers that are over the age of 40.
Parent topic: Clustered Caches (accessible from multiple JVMs)
Replicated Cache
Example 17-20 uses the replicated-scheme
element to define a clustered cache in which a copy of each cache entry is stored on all cluster nodes.
The In-memory Cache sample 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 17-20 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>
Parent topic: Clustered Caches (accessible from multiple JVMs)
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 17-21 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>
Parent topic: Clustered Caches (accessible from multiple JVMs)