网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springboot中怎么利用hazelcast缓存中间件-创新互联

本篇文章给大家分享的是有关springboot中怎么利用hazelcast缓存中间件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联公司于2013年成立,是专业互联网技术服务公司,拥有项目网站设计、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元麻城做网站,已为上家服务,为麻城各地企业和个人服务,联系电话:028-86922220

添加依赖包

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统一配置

@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(3600);
  allUsersCache.setEvictionPolicy(EvictionPolicy.LFU);
  config.getMapConfigs().put("alluserscache", allUsersCache);
  MapConfig usercache = new MapConfig();
  usercache.setTimeToLiveSeconds(3600);//超时时间为1小时
  usercache.setEvictionPolicy(EvictionPolicy.LFU);
  config.getMapConfigs().put("usercache", usercache);//usercache为缓存的cachename
  return config;
 }
}

添加仓储

public interface UserRepository {
 List fetchAllUsers();
 List fetchAllUsers(String name);
}
@Repository
@Profile("hazelcast-cache")// 指定在这个hazelcast-cache环境下,UserRepository的实例才是UserInfoRepositoryHazelcast
public class UserInfoRepositoryHazelcast implements UserRepository {
 @Override
 @Cacheable(cacheNames = "usercache", key = "#root.methodName")// 无参的方法,方法名作为key
 public List fetchAllUsers(){
  List 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}") // 方法名和参数组合做为key
 public List fetchAllUsers(String name) {
  List 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;
 }
}

配置profile

application.yml开启这个缓存的环境

profiles.active: hazelcast-cache

运行程序

可以在单元测试里进行测试,调用多次,方法体只进入一次,这就是缓存成功了。

@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();
 }
}

以上就是springboot中怎么利用hazelcast缓存中间件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网站标题:springboot中怎么利用hazelcast缓存中间件-创新互联
链接地址:http://bjjierui.cn/article/cdcpdi.html

其他资讯