JCache (Payara 4.1.154) - khasunuma/Payara GitHub Wiki

Contents

1. Overview

This page covers how to use the JCache functionality in Payara 4.1.153.
JSR107 (JCache) is implemented in Payara by Hazelcast.

2. Documentation Conventions

Any paths listed throughout the documentation will use the Unix/Linux file path structure (forward slashes).

3. Using JCache in your Applications

The following section will detail how to access and use JCache in your code.

3.1 Accessing the JSR107 Caching Provider and Cache Manager

To create a Cache, you will need a CachingProvider and CacheManager. The embedded Hazelcast member in Payara has a CachingProvider and CacheManager registered to JNDI, so you do not have to create your own. To access them, import the following classes and initialise two variables like so:

import javax.cache.spi.CachingProvider;
import javax.cache.CacheManager;
...
Context ctx = new InitialContext();
CachingProvider provider = (CachingProvider) ctx.lookup("payara/CachingProvider");
CacheManager manager = (CacheManager) ctx.lookup("payara/CacheManager");

3.1.1 Using Injection

You can also use injection to access and use the CachingProvider and CacheManager embedded in Payara. Inject them into your application like so (note, your war or jar must be an implicit or explicit bean archive i.e. contains a CDI Bean with a bean defining annotation, an EJB Session Bean or a beans.xml file):

import javax.cache.CacheManager;
import javax.cache.spi.CachingProvider;
import javax.inject.Inject;
...
@Inject 
CacheManager manager;
@Inject
CachingProvider provider;

3.2 Creating a Cache using Injection

You can create a cache using either the getCache method of a CacheManager, or using injection. Creating the cache with injection uses the Caching Provider and Cache Manager described above.

Creating a cache with injection can be done like so:

import javax.inject.Inject;
import javax.cache.Cache;
...
@Inject
Cache cache;

The name of this cache will be the canonical name of the class it is created in. Caches created in this way will also have JMX statistics and management enabled.

3.2.1 Creating a custom Cache using Injection

You can determine the name and other attributes of a cache created through injection using the @NamedCache annotation.

You can specify the desired custom values as a comma separated list of parameters of the NamedCache annotation when creating a cache.

For example, to inject a cache with a custom name and with JMX management enabled:

import fish.payara.cdi.jsr107.impl.NamedCache;
import javax.inject.Inject;
import javax.cache.Cache;
...
@NamedCache(cacheName = "custom", managementEnabled = true)
@Inject
Cache cache;

The full array of parameters can be seen in the NamedCache section of the appendices.

3.3 Using JCache Annotations

Payara has the necessary interceptors implemented for allowing the full set of JCache annotations to be used.

The JCache annotations are as follows:

  • @CachePut - Puts the specified key and value in the cache.
  • @CacheRemove - Removes the specified key and value from the cache.
  • @CacheResult - Retrieves the value associated with the specified key.
  • @CacheRemoveAll - Removes all keys and values from the cache.
  • @CacheDefaults - Allows the configuration of defaults for CacheResult, CachePut, CacheRemove, and CacheRemoveAll at the class level.
  • @CacheKey - Marks a method parameter as the key of a cache.
  • @CacheValue - Marks a method parameter as the value of a cache key.

4. Appendices

4.1 NamedCache Annotation

Configuration Option Description Default Value
String cacheName Sets the name of the cache. The canonical name of the class receiving the injected cache.
Class keyClass Sets the class of the cache keys. Object.class
Class valueClass Sets the class of the cache values. Object.class
boolean statisticsEnabled Enables or disables JMX statistics. false
boolean managementEnabled Enables or disables JMX management. false
boolean readThrough Enables or disables cache read through. If set to true, a CacheLoader factory class must be specified. false
boolean writeThrough Enables or disables cache write through. If set to true, a CacheWriter factory class must be specified. false
Class cacheLoaderFactoryClass Sets the CacheLoader factory class to be attached to the cache. If not specified, this option is not used.
Class cacheWriterFactoryClass Sets the CacheWriter factory class to be attached to the cache. If not specified, this option is not used.
Class expiryPolicyFactoryClass Sets the ExpiryPolicy factory class to be attached to the cache. If not specified, this option is not used.
⚠️ **GitHub.com Fallback** ⚠️