HazelCast的Spring-Boot和Cache抽象

以前,我们是使用spring提供的默认Cache Manager来开始Spring Cache抽象的。

尽管这种方法可能适合我们对简单应用程序的需求,但是在出现复杂问题的情况下,我们需要使用具有更多功能的其他工具。 Hazelcast就是其中之一。 当涉及到基于JVM的应用程序时,Hazelcast是一个很好的缓存工具。 通过使用hazelcast作为缓存,数据可以在计算机群集的节点之间平均分配,从而可以对可用存储进行水平扩展。

我们将使用spring配置文件运行代码库,因此“ hazelcast-cache”将是我们的配置文件名称。

group 'com.gkatzioura'
version '1.0-SNAPSHOT'


buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

repositories {
    mavenCentral()
}


sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-cache")
    compile("org.springframework.boot:spring-boot-starter")
    compile("com.hazelcast:hazelcast:3.7.4")
    compile("com.hazelcast:hazelcast-spring:3.7.4")

    testCompile("junit:junit")
}

bootRun {
    systemProperty "spring.profiles.active", "hazelcast-cache"
}

如您所见,我们更新了上一个示例中的gradle文件,并添加了两个额外的依赖项hazelcast和hazelcast-spring。 此外,我们还更改了应用程序默认运行的配置文件。

我们的下一步是配置hazelcast缓存管理器。

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.
 */
@Configuration
@Profile("hazelcast-cache")
public class HazelcastCacheConfig {

    @Bean
    public Config hazelCastConfig() {

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

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

        MapConfig usercache = new MapConfig();
        usercache.setTimeToLiveSeconds(20);
        usercache.setEvictionPolicy(EvictionPolicy.LFU);
        config.getMapConfigs().put("usercache",usercache);

        return config;
    }

}

我们刚刚创建了两个具有20秒ttl策略的地图。 因此,自填充地图以来20秒,将发生缓存逐出。 有关更多的hazelcast配置,请参阅官方的hazelcast 文档

我们必须实现的另一项更改是将UserPayload更改为可序列化的Java对象,因为存储在hazelcast中的对象必须是可序列化的。

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;
    }
}

最后但并非最不重要的一点是,我们添加了另一个绑定到hazelcast-cache配置文件的存储库。

结果就是我们先前的与hazelcast集成的spring-boot应用程序,而不是使用ttl策略配置的默认缓存。

您可以在github上找到源代码。

翻译自: https://www.javacodegeeks.com/2017/01/spring-boot-cache-abstraction-hazelcast.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值