springboot~hazelcast caching Middleware

Keywords: Java Spring SpringBoot Redis

Cache is coming.

In the dotnet platform, there is its own cache framework. Of course, there are many integrations in java springboot, and there are many choices for cache middleware. Redis and hazelcast are distributed cache middleware. Today, we mainly talk about the implementation of the latter.

Add dependency package

dependencies {
    compile("org.springframework.boot:spring-boot-starter-cache")
    compile("com.hazelcast:hazelcast:3.7.4")
    compile("com.hazelcast:hazelcast-spring:3.7.4")
}
bootRun {
    systemProperty "spring.profiles.active", "hazelcast-cache"
}

config unified configuration

@Configuration
@Profile("hazelcast-cache")//Name of operation environment
public class HazelcastCacheConfig {

  @Bean
  public Config hazelCastConfig() {

    Config config = new Config();
    config.setInstanceName("hazelcast-cache");

    MapConfig allUsersCache = new MapConfig();
    allUsersCache.setTimeToLiveSeconds(3600);
    allUsersCache.setEvictionPolicy(EvictionPolicy.LFU);
    config.getMapConfigs().put("alluserscache", allUsersCache);

    MapConfig usercache = new MapConfig();
    usercache.setTimeToLiveSeconds(3600);//Timeout is 1 hour
    usercache.setEvictionPolicy(EvictionPolicy.LFU);
    config.getMapConfigs().put("usercache", usercache);//usercache is the cache name

    return config;
  }

}

Add storage

public interface UserRepository {

  List<UserInfo> fetchAllUsers();

  List<UserInfo> fetchAllUsers(String name);
}


@Repository
@Profile("hazelcast-cache")// Specify that in this hazelcast cache environment, the instance of UserRepository is userinforepository hazelcast
public class UserInfoRepositoryHazelcast implements UserRepository {

  @Override
  @Cacheable(cacheNames = "usercache", key = "#root.methodName ") / / a method without parameters. The method name is the key
  public List<UserInfo> fetchAllUsers(){
    List<UserInfo> list = new ArrayList<>();
    list.add(UserInfo.builder().phone("135").userName("zzl1").createAt(LocalDateTime.now()).build());
    list.add(UserInfo.builder().phone("136").userName("zzl2").createAt(LocalDateTime.now()).build());
    return list;
  }
  @Override
  @Cacheable(cacheNames = "usercache", key = "{#Name} ") / / method name and parameter combination as key
  public List<UserInfo> fetchAllUsers(String name) {
    List<UserInfo> list = new ArrayList<>();
    list.add(UserInfo.builder().phone("135").userName("zzl1").createAt(LocalDateTime.now()).build());
    list.add(UserInfo.builder().phone("136").userName("zzl2").createAt(LocalDateTime.now()).build());
    return list;
  }
}

Configure profile

application.yml opens the cache environment

  profiles.active: hazelcast-cache

Operation procedure

It can be tested in unit test, called many times, and the method body only enters once, which means the cache is successful.

@ActiveProfiles("hazelcast-cache")
public class UserControllerTest extends BaseControllerTest {
  @Test
  public void fetchUsers() {
    getOk();
    //test caching
    getOk();
  }

  private WebTestClient.ResponseSpec getOk() {
    return http.get()
        .uri("/users/all/zzl")
        .exchange()
        .expectStatus().isOk();
  }
}

Posted by Shaudh on Wed, 01 Jan 2020 11:37:00 -0800