Tag: Java Cache Management

CACHE MANAGEMENT IN JAVA – CACHING TECHNIQUES

Introduction

This post is intended by java application development professionals to make you learn about the tricks to design the caching system in java. The samples that professionals are sharing in this article will help you in gathering knowledge of the caching system and you will be able to design it for your project.

What is Cache?

The Cache is a local area where we can access the data very fast.  We can use cache management in all applications for better performance.

Let’s say you have a rest web Service or Database in which you are fetching Customer details and returning Customer Profile data. Whenever call the Customer Profile API, it will go to a third-party reset server or database then, fetch the customer data and return back to our destination system.  Let’s assume that there are several thousands of people in the portal calling many times the same Customer Profile API. Each time the Customer Profile API invokes and fetches data from the Rest Service or Database. This process is too expensive. This will bring down the system’s performance and the response time of the System will increase. To avoid this situation we will use Cache management.

Following is a System diagram without caching layer (diagram 1)

Following is a system diagram with caching layer (diagram 2)

Caching layer’s responsibility is to take the data from the destination System and store all those data in memory. Most caching system stores the data in key-value pair. It is very similar to Java HashMap but, it is very big with the option to configure some useful features on caching data, such as lifetime and Ideal time period, etc.

Based on the second diagram, the client system looks for the data first in caching system, if data is exist in the caching system, it will respond to the client system. If requested data is not found or not the latest data, the request goes to the rest service or database server for new data then, updates the data in the caching system.

This process happened in the following two cases.

  1. If the data request is the very first time.
  2. The requested data is very old or outdated data.

There are many open-source caching systems in the market. All are having their own advantages and disadvantages so, your responsibility to pick the right caching system for your project requirement. I am implementing the above cache system (diagram 2) through the EH cache management.

Following is the ehcache.xml file configuration

<ehcache>

<diskStorepath=“java.io.tmpdir”/>

<cachename=“test-cache”

maxEntriesLocalHeap=“10000”

eternal=“false”

timeToIdleSeconds=“120”

timeToLiveSeconds=“120”

overflowToDisk=“true”

maxEntriesLocalDisk=“10000000”

diskExpiryThreadIntervalSeconds=“120”

memoryStoreEvictionPolicy=“LRU”>

</cache>

</ehcache>

Above is XML based configuration and also, we can do configuration by programmatically like below.

Cache cache = manager.getCache(“test-cache”);

CacheConfigurationconfig = cache.getCacheConfiguration();

config.setTimeToIdleSeconds(60);

config.setTimeToLiveSeconds(120);

config.setMaxEntriesLocalHeap(10000);

config.setMaxEntriesLocalDisk(1000000);

config.setEternal(true);

config.setOverflowToDisk(true);

config.setDiskExpiryThreadIntervalSeconds(120);

config.setMemoryStoreEvictionPolicyFromObject(MemoryStoreEvictionPolicy.LRU);

Here, I am explaining the exact use of caching configuration attributes.

maxEntriesLocalHeap

Using this property we can tell how much cached data can keep in the Cache System.

timeToIdleSeconds

If any cached data is not used by the application, those cached data consider idle cached data.  Using this property we can set an idle time period for cached data. For example, if timeToIdleSeconds =”120”, the cached data can be idle till 120 seconds, after that cached data is destroyed from the memory.

timeToLiveSeconds

Using this property we can set the lifetime of cached data. If you set 500, the lifetime of cached data is 500 seconds, after that cached data will be destroyed from the memory.

Eternal

If you want to override or change the value of timeToIdleSeconds and timeToLiveSeconds, eternal should be true. If this is true, dynamically you can change idle and a lifetime of cached data.

overflowToDisk

If the cached memory reached its maximum, the cached old data write-in system’s physical disk.

maxEntriesLocalDisk

Using this property you can set the maximum local Disk storage limit for your cached data.

diskExpiryThreadIntervalSeconds

The thread runs and monitors to check the life of disk cached data whether expired or not. If expired, those data are destroyed from the disk. This thread runs by default in 2 seconds interval. You can change based on your necessity.

memoryStoreEvictionPolicy

Using this property we can set the following one of policy on your caching system based on your requirement.

  • LFU (Least Frequently Used) – the default
  • LRU (Least Recently Used)
  • FIFO (First In, First Out)

LFU (Least Frequently Used)

This policy checks that minimal frequently used items by the application in the cache System and destroys those items from the memory. For example, you have two items in the cache memory, one used by 50 users and the second one used by 10 users. This policy removes the second item.

LRU (Least Recently Used)

This type of caching policy checks the cached data which are very minimal used recently. For example, you have two items in the cache system, one used by 50 users and the second one used by 10 users; again the first one was used by 2 users, so the first item was removed from the cache system.

FIFO (First In, First Out)

This policy removes the items that were placed in the cache first. For example, if you have 100 items in the cache. If the cache system reaches the limit, the 1st item is removed from the cache system.

Below is the sample program which looks up the data from the cache, if the data do not exist in cache systems, the application picks up the data from the database and adds those data to the cache system.

Following method to get cache details which configured in ehcache.xml

public Cache getCache() {

CacheManagermanager = CacheManager.newInstance(“ehcache.xml”);

Cache cache = manager.getCache(“test-cache”);

returncache;

}

Following method to get the cache data from the cache system.

public Customer getCustomerData(longcustomerId) {

Customer customer = null;

Cache cache = getCache();

Element element = cache.get(customerId);

if(element != null) {

customer = (Customer)element.getObjectValue();

}

returncustomer;

}

Following method to add the data to cache system

publicvoidaddDataToCache(Customer customer) {

Cache cache = getCache();

Element element = new  Element(customer.getCustomerId(),customer);

cache.put(element);

}

Following code fetch the customer data from database

@Transactional

public Customer getCustomerById(longcustomerId) {

Customer customer = null;

try{

Session session = sessionFactory.getCurrentSession();

Query query = session.createQuery(“from Customer customer where customer.customerId=:customerId”);

query.setLong(“customerId”, customerId);

List<Customer>list = query.list();

if(list != null){

customer = list.get(0);

}

}catch(Exception ex){

ex.printStackTrace();

}

returncustomer;

}

Read More: The Java Web Development Trends to Pick Momentum in 2018

Client code

Following the client, the code uses the above methods. This client code checks the Cache System for requested data. If data is not found, it will look up the data in the database then, display it to the customer and those data in Cache System for the customer’s future request.

Customercustomer = null;

CachedDatacachedData = newCachedData();

customer = cachedData.getCustomerData(1);

if(customer != null) {

System.out.println(customer.getCustomerId() + ” ,  ” + customer.getCustomerName() + ” , ” + customer.getEmailId());

}else{

AbstractApplicationContextctx = newClassPathXmlApplicationContext(“classpath:applicationContext.xml”);

CustomerDaocustomerDao = (CustomerDao)ctx.getBean(“customerDao”);

customer = customerDao.getCustomerById(1);

System.out.println(customer.getCustomerId() + ” ,  ” + customer.getCustomerName() + ” , ” + customer.getEmailId());

cachedData.addDataToCache(customer);

}

I hope you understand how to design the caching system for your project. The above samples are simple caching systems, but it gives you a good startup for your caching system design for your project.

Java application development professionals hope you have completely understood the cache management design. You can create your design on your own and share your experience with our readers. For any doubt or query, write to the experts and get answers.