Spring Boot and Cache Abstraction With Hazelcast

DZone 's Guide to

Spring Boot and Cache Abstraction With Hazelcast

Once you've mastered the basics of cache abstraction with Spring, consider moving onto Hazelcast for more options while incorporating a time-to-live policy.

· Java Zone ·
Free Resource

Previously we got started with Spring cache abstraction using the default cache manager that Spring provides.

Although this approach might suit our needs for simple applications, in the case of complex problems, we need to use different tools with more capabilities. Hazelcast is one of them. Hazelcast is hands down a great caching tool when it comes to a JVM-based application. By using Hazelcast as a cache, data is evenly distributed among the nodes of a computer cluster, allowing for horizontal scaling of available storage.

We will run our codebase using spring profiles thus ‘hazelcast-cache’ will be our profile name.

group 'com.gkatzioura'
version '1.0-SNAPSHOT'
buildscript {
    repositories {
    dependencies {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
repositories {
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
bootRun {
    systemProperty "spring.profiles.active", "hazelcast-cache"

As you can see, we updated the Gradle file from the previous example and added two extra dependencies — hazelcast and hazelcast-spring. Also, we changed the profile that our application will run by default.

Our next step is to configure the Hazelcast cache manager.

package com.gkatzioura.caching.config;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.MapConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
 * Created by gkatzioura on 1/10/17.
public class HazelcastCacheConfig {
    public Config hazelCastConfig() {
        Config config = new Config();
        MapConfig allUsersCache = new MapConfig();
        MapConfig usercache = new MapConfig();
        return config;

We just created two maps with a TTL policy of 20 seconds. Therefore, 20 seconds after the map gets populated, a cache eviction will occur. For more Hazelcast configurations, please refer to the official  documentation.

Another change that we have to implement is to change UserPayload into a serializable Java object, since objects stored in Hazelcast must be serializable.

package com.gkatzioura.caching.model;
import java.io.Serializable;
 * Created by gkatzioura on 1/5/17.
public class UserPayload implements Serializable {
    private String userName;
    private String firstName;
    private String lastName;
    public String getUserName() {
        return userName;
    public void setUserName(String userName) {
        this.userName = userName;
    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;

Last but not least, we add another repository bound to the hazelcast-cache profile.

The result is our previous spring-boot application integrated with Hazelcast instead of the default cache, configured with a TTL policy.

You can find the source code on GitHub.

cache abstraction ,hazelcast ,java ,ttl ,tutorial

Published at DZone with permission of Emmanouil Gkatziouras , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}