珠海高端网站制作公司,极简风格的网站,wordpress安装与使用说明,百度推广app怎么收费hazelcast以前#xff0c;我们是使用spring提供的默认Cache Manager来开始Spring Cache抽象的。 尽管这种方法可能适合我们对简单应用程序的需求#xff0c;但是在出现复杂问题的情况下#xff0c;我们需要使用具有更多功能的其他工具。 Hazelcast就是其中之一。 当涉及到基… hazelcast 以前我们是使用spring提供的默认Cache Manager来开始Spring Cache抽象的。 尽管这种方法可能适合我们对简单应用程序的需求但是在出现复杂问题的情况下我们需要使用具有更多功能的其他工具。 Hazelcast就是其中之一。 当涉及到基于JVM的应用程序时Hazelcast是一个很好的缓存工具。 通过使用hazelcast作为缓存数据可以在计算机群集的节点之间平均分配从而可以对可用存储进行水平扩展。 我们将使用spring配置文件运行代码库因此“ hazelcast-cache”将是我们的配置文件名称。 group com.gkatzioura
version 1.0-SNAPSHOTbuildscript {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.bootrepositories {mavenCentral()
}sourceCompatibility 1.8
targetCompatibility 1.8dependencies {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 {Beanpublic 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.htmlhazelcast