JCache simple example

This post will be a simple introduction to JCache api. For this post and other future post related to JCache, I will be using Ehcache (a tool that provides caching facility).

For the below example we need to know about the below terms.
1) CachingProvider
2) CacheManager
3) Cache
4) Configuration

CachingProvider –> Represents the cache provider in this case Ehcache. Used to create, manange, and control one or more CacheManager.

CacheManager –> A manager class whose responsibility is to create, manage, and control multiple caches.

Cache –> A named region which stores multiple data as key value pair.

Configuration –> A interface through which we can provide configuration information. We can configure the way our cache should behave or created using this interface. Their will be a many to one relationship between Cache and Configuration interface.

Below is the complete code which will create a cache by name “cache1” and store 4 key value pair data. We will then iterate through the cache data using an iterator

Main Class Code


1   import java.util.Iterator;
2   
3   import javax.cache.Cache;
4   import javax.cache.Cache.Entry;
5   import javax.cache.CacheManager;
6   import javax.cache.Caching;
7   import javax.cache.configuration.MutableConfiguration;
8   import javax.cache.spi.CachingProvider;
9   
10  public class JCacheDemo1 {
11      public static void main(String[] args) {
12          CachingProvider cachingProvider = Caching.getCachingProvider();
13          CacheManager cacheManager = cachingProvider.getCacheManager();
14          
15          MutableConfiguration mutableConfiguration = new MutableConfiguration();
16          mutableConfiguration.setTypes(String.class, String.class);
17          
18          Cache cache = cacheManager.createCache("cache1", mutableConfiguration);
19          cache.put("key1", "value1");
20          cache.put("key2", "value2");
21          cache.put("key3", "value3");
22          cache.put("key4", "value4");
23          
24          Iterator iterator = cache.iterator();
25          while(iterator.hasNext()) {
26              Entry entry = iterator.next();
27              System.out.println(entry.getKey() + ":" + entry.getValue());
28          }
29          
30          cachingProvider.close();
31      }
32  }

Explanation

In the above example I have used default caching provider and default cache manager.

We can obtain the default caching provider (which is Ehcache in this case) using the code shown below. Refer to line 12 in the main code.
CachingProvider cachingProvider = Caching.getCachingProvider();

From the default CachingProvider we get the defualt CacheManager using the code shown below. Refer to line 13 in the main code.
CacheManager cacheManager = cachingProvider.getCacheManager();

I will be covering more about default CachingProvider and CacheManager in future posts.

Now we provide details which will be used to configure the cache we will create. We will use an instance of MutableConfiguration class which implements Configuration interface. We create an instance using the below code snippet.
Refer to line 15 in the main code.
MutableConfiguration mutableConfiguration = new MutableConfiguration();

We now set the datatype of the key and value using the code as shown below. Refer to line 16 in the main code
mutableConfiguration.setTypes(String.class, String.class);

Now we create a Cache region named “cache1” and using the configuration instance created previously as shown below. Refer to line 18 in the main code
Cache cache = cacheManager.createCache(“cache1”, mutableConfiguration);

Now we add values using put method provided in the cache class.
cache.put(“key1”, “value1”);
cache.put(“key2”, “value2”);
cache.put(“key3”, “value3”);
cache.put(“key4”, “value4”);

Next we iterate through the cache data using the iterator. Refer to line 24 to 28 in the main code

At the end we close and destroy all the caches, cacheManagers, and cachingProvider by calling close method on cachingProvider instance. Refer to line 30 in the main code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s