In this post I'm going to demonstrate how to use JCache which provide memory caching functionality based on the JCache JSR (JSR-107).
So in this post I'll describe very simple instructions to initialize JCache and how to load data in to memory, retrieve data from memory and remove data from memory cache. Note that this is very simple introduction to JCache cache management and I will provide move advanced features in future posts.
JCache is a standard caching API for Java. It provides an API for applications to be able to create and work with in-memory cache of user defined objects. There are many advantages in JCache such as – developers does not need to concentrate on the finer details of implementing the Caching and that time can spent on the development of the core business logic in the application.
Prerequisites
- You should have install java 1.6 or above.
- You should have Eclipse installed in your PC.
- Your PC should setup Maven installed and configured.
First lets create maven project in Eclipse. Go to File -> New -> Maven Project then tick the check fox for the Create Simple Project (Skip archetype selection) and then click next. On next screen just provide some name for Group ID and Artifact ID (as show on below figure) then click finished.
Figure 1 : Providing Artifact Id as HazelcastForBeginer |
Then set project run time environment as JDK according to your local JAVA installation. You can change the runtime environment Right click project the go to Build Path -> Configure Build path .. then on Libraries tab click on JRE System Library and remove existing one. Then add new JDK reference. So final configuration will be similar to Figure
Figure 2 : Changing the JRE in project |
Figure 3 : Set JDK in run time environment for project |
After that make sure you change Java Compiler in to 1.8 you can change it on Java Compiler in project properties. As show on below figure.
Figure 4: Change Compiler to 1.7 or above |
Then needed to add required relevant dependencies. For this example we required only following two dependencies.
<dependencies>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.9-EA</version>
</dependency>
</dependencies>
package com.test.hazelcast;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;
public class HazelcastMain {
public static void main(String[] args) {
HazelcastMain mainApp=new HazelcastMain();
String key="val1";
mainApp.addOrUpdateCache(key, "test1");
String value = mainApp.getValue(key);
System.out.println("value from cache : "+ value);
mainApp.addOrUpdateCache(key, "test2");
value = mainApp.getValue(key);
System.out.println("value after update: "+ value);
mainApp.removeFromCache(key);
value = mainApp.getValue(key);
System.out.println("value after remove: "+ value);
}
private String getValue(String key){
Cache<String, String> cache = getChacheManager();
return cache.get(key);
}
private void removeFromCache(String key){
Cache<String, String> cache = getChacheManager();
cache.remove(key);
}
private void addOrUpdateCache(String key, String value){
Cache<String, String> cache = getChacheManager();
cache.put(key, value);
}
private Cache<String, String> getChacheManager(){
CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();
MutableConfiguration<String, String> config
= new MutableConfiguration<>();
Cache<String, String> cache = cacheManager
.getCache("simpleCache");
if(cache==null){
cache = cacheManager
.createCache("simpleCache", config);
}
return cache;
}
}
Then run the application. You should see similar result to below out put.
Figure 5 : Output of the application |
You can see it has initially it added "test1" value in to cache then it has updated to "test2" then after we remove it from cache it returned "null" value.