このコンテンツは選択した言語では利用できません。
24.2. Using the Infinispan CDI Module
- To configure and inject Infinispan caches into CDI Beans and Java EE components.
- To configure cache managers.
- To control storage and retrieval using CDI annotations.
24.2.1. Configure and Inject Infinispan Caches リンクのコピーリンクがクリップボードにコピーされました!
24.2.1.1. Inject an Infinispan Cache リンクのコピーリンクがクリップボードにコピーされました!
public class MyCDIBean {
@Inject
Cache<String, String> cache;
}
public class MyCDIBean {
@Inject
Cache<String, String> cache;
}
24.2.1.2. Inject a Remote Infinispan Cache リンクのコピーリンクがクリップボードにコピーされました!
public class MyCDIBean {
@Inject
RemoteCache<String, String> remoteCache;
}
public class MyCDIBean {
@Inject
RemoteCache<String, String> remoteCache;
}
24.2.1.3. Set the Injection's Target Cache リンクのコピーリンクがクリップボードにコピーされました!
- Create a qualifier annotation.
- Add a producer class.
- Inject the desired class.
24.2.1.3.1. Create a Qualifier Annotation リンクのコピーリンクがクリップボードにコピーされました!
Example 24.1. Custom Cache Qualifier
@javax.inject.Qualifier
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SmallCache {}
@javax.inject.Qualifier
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SmallCache {}
@SmallCache qualifier to specify how to create specific caches.
24.2.1.3.2. Add a Producer Class リンクのコピーリンクがクリップボードにコピーされました!
@SmallCache qualifier (created in the previous step) specifies a way to create a cache:
Example 24.2. Using the @SmallCache Qualifier
@ConfigureCachespecifies the name of the cache.@SmallCacheis the cache qualifier.
24.2.1.3.3. Inject the Desired Class リンクのコピーリンクがクリップボードにコピーされました!
@SmallCache qualifier and the new producer class to inject a specific cache into the CDI bean as follows:
public class MyCDIBean {
@Inject @SmallCache
Cache<String, String> mySmallCache;
}
public class MyCDIBean {
@Inject @SmallCache
Cache<String, String> mySmallCache;
}
24.2.2. Configure Cache Managers with CDI リンクのコピーリンクがクリップボードにコピーされました!
24.2.2.1. Specify the Default Configuration リンクのコピーリンクがクリップボードにコピーされました!
Example 24.3. Specifying the Default Configuration
Note
@Default qualifier if no other qualifiers are provided.
@Produces annotation is placed in a method that returns a Configuration instance, the method is invoked when a Configuration object is required.
24.2.2.2. Override the Creation of the Embedded Cache Manager リンクのコピーリンクがクリップボードにコピーされました!
After a producer method is annotated, this method will be called when creating an EmbeddedCacheManager, as follows:
Example 24.4. Create a Non Clustered Cache
@ApplicationScoped annotation specifies that the method is only called once.
The following configuration can be used to create an EmbeddedCacheManager that can create clustered caches.
Example 24.5. Create Clustered Caches
The method annotated with @Produces in the non clustered method generates Configuration objects. The methods in the clustered cache example annonated with @Produces generate EmbeddedCacheManager objects.
EmbeddedCacheManager and injects it into the code at runtime.
Example 24.6. Generate an EmbeddedCacheManager
... @Inject EmbeddedCacheManager cacheManager; ...
...
@Inject
EmbeddedCacheManager cacheManager;
...
24.2.2.3. Configure a Remote Cache Manager リンクのコピーリンクがクリップボードにコピーされました!
RemoteCacheManager is configured in a manner similar to EmbeddedCacheManagers, as follows:
Example 24.7. Configuring the Remote Cache Manager
24.2.2.4. Configure Multiple Cache Managers with a Single Class リンクのコピーリンクがクリップボードにコピーされました!
Example 24.8. Configure Multiple Cache Managers
24.2.3. Storage and Retrieval Using CDI Annotations リンクのコピーリンクがクリップボードにコピーされました!
24.2.3.1. Configure Cache Annotations リンクのコピーリンクがクリップボードにコピーされました!
javax.cache package.
Important
24.2.3.2. Enable Cache Annotations リンクのコピーリンクがクリップボードにコピーされました!
beans.xml file.
Adding the following code adds interceptors such as the InjectedCacheResultInterceptor, InjectedCachePutInterceptor, InjectedCacheRemoveEntryInterceptor and the InjectedCacheRemoveAllInterceptor:
Example 24.9. Adding CDI Interceptors
Adding the following code adds interceptors such as the CacheResultInterceptor, CachePutInterceptor, CacheRemoveEntryInterceptor and the CacheRemoveAllInterceptor:
Example 24.10. Adding JCache Interceptors
Note
beans.xml file for Red Hat JBoss Data Grid to use javax.cache annotations.
24.2.3.3. Caching the Result of a Method Invocation リンクのコピーリンクがクリップボードにコピーされました!
toCelsiusFormatted method again and stores the result in the cache.
@CacheResult annotation instead, as follows:
toCelsiusFormatted() method call.
Note
24.2.3.3.1. Specify the Cache Used リンクのコピーリンクがクリップボードにコピーされました!
cacheName) to the @CacheResult annotation to specify the cache to check for results of the method call:
@CacheResult(cacheName = "mySpecialCache")
public String doSomething(String parameter) {
<!-- Additional configuration information here -->
}
@CacheResult(cacheName = "mySpecialCache")
public String doSomething(String parameter) {
<!-- Additional configuration information here -->
}
24.2.3.3.2. Cache Keys for Cached Results リンクのコピーリンクがクリップボードにコピーされました!
@CacheResult annotation creates a key for the results fetched from a cache. The key consists of a combination of all parameters in the relevant method.
@CacheKey annotation as follows:
Example 24.11. Create a Custom Key
p1 and p2 are used to create the cache key. The value of dontCare is not used when determining the cache key.
24.2.3.3.3. Generate a Custom Key リンクのコピーリンクがクリップボードにコピーされました!
cacheKeyGenerator to the @CacheResult annotation as follows:
@CacheResult(cacheKeyGenerator = MyCacheKeyGenerator.class)
public void doSomething(String p1, String p2) {
<!-- Additional configuration information here -->
}
@CacheResult(cacheKeyGenerator = MyCacheKeyGenerator.class)
public void doSomething(String p1, String p2) {
<!-- Additional configuration information here -->
}
p1 contains the custom key.
24.2.4. Cache Operations リンクのコピーリンクがクリップボードにコピーされました!
24.2.4.1. Update a Cache Entry リンクのコピーリンクがクリップボードにコピーされました!
@CachePut annotation is invoked, a parameter (normally passed to the method annotated with @CacheValue) is stored in the cache.
Example 24.12. Sample @CachePut Annotated Method
cacheName and cacheKeyGenerator in the @CachePut method. Additionally, some parameters in the invoked method may be annotated with @CacheKey to control key generation.
24.2.4.2. Remove an Entry from the Cache リンクのコピーリンクがクリップボードにコピーされました!
@CacheRemoveEntry annotated method that is used to remove an entry from the cache:
Example 24.13. Removing an Entry from the Cache
cacheName and cacheKeyGenerator attributes.
24.2.4.3. Clear the Cache リンクのコピーリンクがクリップボードにコピーされました!
@CacheRemoveAll method to clear all entries from the cache.
Example 24.14. Clear All Entries from the Cache with @CacheRemoveAll
cacheName attribute.