Creating a Cache Instance

You can create a cache using either the getCache method of the CacheManager, or directly injecting into your bean. Creating the cache with injection uses the CachingProvider and CacheManager components.

Using Injection

Injecting a cache into a managed bean is simple enough:

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.

Typed Cache Injection

Since Payara Server 4.1.1.164

A typed cache can also be injected in the same manner:

import javax.inject.Inject;
import javax.cache.Cache;
...
@Inject
Cache<Long, Property> cache;
Both key and value types must be Serializable so that the cache can be injected correctly.

Injecting a Custom Cache

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;

If you only want to set the name of the cache but don’t want to depend on the @NamedCache annotation since it’s part of the Payara Extras dependencies, you can use the @CacheDefaults annotation on the bean class:

import javax.inject.Inject;
import javax.cache.Cache;
import javax.cache.annotation.CacheDefaults;
import javax.enterprise.context.ApplicationScoped;
...
@ApplicationScoped
@CacheDefaults(cacheName = "custom")
public class CacheBean {
    ...
    @Inject
    Cache cache;
    ...
}

Keep in mind that this solution only works if your bean has one injected cache only. If you are in a situation where you must inject more than one cache into the bean then consider using the @NamedCache annotation to avoid name collisions.