EhCache3 as JCache (JSR-107) Implementation with Cache Statistics

EhCache3 as JCache (JSR-107)

If you are reading this you already know that caching is mechanism to store static data In-Memory for faster access and avoid expensive database calls or any kind of service calls to get the data.

Read my other post to learn InMemory Caching using EhCache3.

There are many ways to implement cache in java based applications, starting from very simple Map based caching to Apache Common JCS, EhCache, Apache Ignite, Hazelcast, Oracle Coherence and many more. With so many implementations available, question is which one we should go for? 

If you have to choose one from multiple options best bet is to look for standard. Standard because many people have put their mind together in it, better support, examples and guide available for reference plus expect that further upgrades/changes will not breaking changes.

So for InMemory cache Standard Specification is JSR-107.  You can read about it in more detail at JSR 107: JCACHE - Java Temporary Caching API. JCache specifies API and semantics for temporary, in memory caching of Java objects, including object creation, shared access, spooling, invalidation, and consistency across JVM's.

About JCache

JCache is just specification, set of interfaces / APIs and there are multiple implementation available from different vendors.  You have have to choose one implementation that you like or prefer.
Some of the implementations are -
  1. Ehcache
  2. Coherence
  3. Hazelcast
  4. Apache Ignite 1.0
For comprehensive list of JCache implementations click here

Subtle Benefits

  1. Irrespective of what implementation you choose you will always use classes and interfaces provided by jcache api. Which means you can change underlying implementation anytime
  2. If you upgrade underlying implementation it should not be breaking your existing code
  3. Access live cache usage statistics at runtime using JMX
  4. You are using Standard API Specification :-)

JCache with EhCache as Implementation

Remember to import and use all caching classes from javax.cache package. There are classes with same names in ehcache package also, make sure you don't use those otherwise you will face issues.

I will showcase example of how to use JCache with EhCache as underlying Implementation. For this example you need to have following jars in the class path - 


I will showcase following basic and very important aspects of InMemory caching - 
Item Expiry - If entry in the cache meets the expiry condition, it will be evicted (removed) from the cache. To standard type of expiry conditions are Time-To-Live and Time-To-Idle. I will use Time-To-Live

Cache Max Size - Maximum size up to which the cache can grow. The max size constraint can be set by byte size (KB, MB etc) or no on entries in the cache. I will use no of entries.

Cache Usage Statistics -  When you implement caching you are certainly interested in knowing cache usage statistics like, cache hits, misses, no of time cache was accessed etc. With JCache you can get all cache usage statistics live at runtime using JMX mechanism.

There are 2 ways to configure cache. XML and programmatic. I will use xml way because it's more readable and intuitive. 
Below is xml configuration for a cache of Person object by personId With max size of 5 entries and Time-To-Live 30 seconds.


The object that we will put in the cache
Person.java

Cache manager helper class which  takes care of initializing cache and implements some utility method to easily access cache and cache usage statistics
AppCacheManager.java

The main program to test caching
MyMain.java
Output
Cache Initialized
Entries in the Cache at: Tue Jan 09 22:10:01 EST 2018
--------------------
Key: 1, Value: null
Key: 2, Value: null
Key: 3, Value: Person [personId=3, firstName=firstName-1, lastName=lastName-3]
Key: 4, Value: Person [personId=4, firstName=firstName-1, lastName=lastName-4]
Key: 5, Value: Person [personId=5, firstName=firstName-1, lastName=lastName-5]
Key: 6, Value: Person [personId=6, firstName=firstName-1, lastName=lastName-6]
Key: 7, Value: Person [personId=7, firstName=firstName-1, lastName=lastName-7]

Waiting for 40 seconds


After 40 seconds wait

Entries in the Cache at: Tue Jan 09 22:10:41 EST 2018
--------------------
Key: 1, Value: null
Key: 2, Value: null
Key: 3, Value: null
Key: 4, Value: null
Key: 5, Value: null
Key: 6, Value: null
Key: 7, Value: null

Cache Usage Statitics
--------------------
CacheHits=5
CacheHitPercentage=35.714287
CacheMisses=9
CacheMissPercentage=64.28571
CacheGets=14
CachePuts=7
CacheRemovals=0
CacheEvictions=2
AverageGetTime=91.180786
AveragePutTime=731.2153
AverageRemoveTime=0.0

Explanation

As you can see we added 7 items in the cache. Since cache max entry is set to 5, we found only 5 entries in the cache, 2 are null which means those are not present in the cache. After waiting for 40 seconds we checked cache again. Based on Time-To-Live of 30 seconds all entries are expired and nothing is found in the cache. Note the self explanatory cache usage statistics in the end.

Do you like it?
You can download all code from github https://github.com/rakeshprajapati1982/ehcache-jcache

As always comments and feedback are welcome.


Comments

Other Popular Posts

How to enable JPA eclipselink logging in WAS Liberty

Java Thread Local Storage explained in easiest practical way

Lambda Expression v/s Anonymous Inner Class - Java 8

EhCache3 In Memory Caching for Performance Improvement