[id='jcache_annotations']
= JCache Caching Annotations
You can use the following JCache caching annotations with CDI managed beans when JCache artifacts are on the classpath:

`@CacheResult`:: caches the results of method calls.
`@CachePut`:: caches method parameters.
`@CacheRemoveEntry`:: removes entries from a cache.
`@CacheRemoveAll`:: removes all entries from a cache.

[IMPORTANT]
====
**Target type:** You can use these JCache caching annotations on methods only.
====

To use JCache caching annotations, declare interceptors in the `beans.xml`
file for your application.

.Managed Environments (Application Server)
[source,xml,options="nowrap",subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   version="1.2" bean-discovery-mode="annotated">

  <interceptors>
    <class>org.infinispan.jcache.annotation.InjectedCacheResultInterceptor</class>
    <class>org.infinispan.jcache.annotation.InjectedCachePutInterceptor</class>
    <class>org.infinispan.jcache.annotation.InjectedCacheRemoveEntryInterceptor</class>
    <class>org.infinispan.jcache.annotation.InjectedCacheRemoveAllInterceptor</class>
  </interceptors>
</beans>
----

.Non-managed Environments (Standalone)
[source,xml,options="nowrap",subs=attributes+]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   version="1.2" bean-discovery-mode="annotated">

  <interceptors>
    <class>org.infinispan.jcache.annotation.CacheResultInterceptor</class>
    <class>org.infinispan.jcache.annotation.CachePutInterceptor</class>
    <class>org.infinispan.jcache.annotation.CacheRemoveEntryInterceptor</class>
    <class>org.infinispan.jcache.annotation.CacheRemoveAllInterceptor</class>
  </interceptors>
</beans>
----

.JCache Caching Annotation Examples

The following example shows how the `@CacheResult` annotation caches the results of the `GreetingService.greet()` method:

[source,java]
----
import javax.cache.interceptor.CacheResult;

public class GreetingService {

    @CacheResult
    public String greet(String user) {
        return "Hello" + user;
    }
}
----

With JCache annotations, the default cache uses the fully qualified name of the
annotated method with its parameter types, for example: +
`org.infinispan.example.GreetingService.greet(java.lang.String)`

To use caches other than the default, use the `cacheName` attribute to specify
the cache name as in the following example:

[source,java]
----
@CacheResult(cacheName = "greeting-cache")
----
