缓存来了

在dotnet平台有自己的缓存框架,在java springboot里当然了集成了很多,而且缓存的中间件也可以进行多种选择,向redis, hazelcast都是分布式的缓存中间件,今天主要说一下后者的实现。

添加依赖包

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<UserInfo> fetchAllUsers();

  List<UserInfo> 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<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}") // 方法名和参数组合做为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;
}
}

配置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缓存中间件的更多相关文章

  1. springboot与缓存(redis,或者caffeine,guava)

    1.理论介绍 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvide ...

  2. 学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用

    学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用 一.SpringBoot系列教程 二.SpringBoot ...

  3. .net 分布式架构之分布式缓存中间件

    开源git地址: http://git.oschina.net/chejiangyi/XXF.BaseService.DistributedCache 分布式缓存中间件  方便实现缓存的分布式,集群, ...

  4. 带着新人学springboot的应用03(springboot+mybatis+缓存 下)

    springboot+mybatis+缓存,基本的用法想必是会了,现在说一说内部大概的原理. 稍微提一下mybatis,只要导入了mybatis的依赖,那么有个自动配置类就会生效,你可以去mybati ...

  5. redis缓存中间件基础

    前序: 默认使用SimpleCacheConfiguration 组件ConcurrentMapCacheManager==ConcurrentMapCache将数据保存在ConcurrentMap& ...

  6. SpringBoot 与缓存

    1. JSR107 Java Caching 定义了5个核心接口: CachingProvider:定义了创建,配置,获取,管理和控制多个CacheManager; CacheManager:定义了创 ...

  7. SpringBoot与缓存

    一.Spring Boot与缓存. JSR-107.Spring缓存抽象.整合Redis 一.JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, Cach ...

  8. 老司机带你玩转面试(1):缓存中间件 Redis 基础知识以及数据持久化

    引言 今天周末,我在家坐着掐指一算,马上又要到一年一度的金九银十招聘季了,国内今年上半年受到 YQ 冲击,金三银四泡汤了,这就直接导致很多今年毕业的同学会和明年毕业的同学一起参加今年下半年的秋招,这个 ...

  9. SpringBoot 整合缓存Cacheable实战详细使用

    前言 我知道在接口api项目中,频繁的调用接口获取数据,查询数据库是非常耗费资源的,于是就有了缓存技术,可以把一些不常更新,或者经常使用的数据,缓存起来,然后下次再请求时候,就直接从缓存中获取,不需要 ...

随机推荐

  1. 两个标签页定位第二个标签页元素时显示element not visible

    问题描述 web页面有两个标签页, 当转换到第二个标签页定位元素时, 显示element not visible. 代码 ... //省略 WebElement ele= browser.getEle ...

  2. search_request.go

    package types type SearchRequest struct {     // 搜索的短语(必须是UTF-8格式),会被分词     // 当值为空字符串时关键词会从下面的Token ...

  3. bzoj 3680 吊打xxx 模拟退火

    第一道模拟退火 重心嘛,就是要找到一个点,使其到所有点距离*该点权值和最小 思路:初始化一个T,mint,当T大于mint时,每次随机一个解,如果解比当前解优,直接转移,否则,以某概率(与T正相关)转 ...

  4. MySQL--各版本DDL 操作总结

    MySQL 5.5 DDL 在MySQL 5.5版本前,所有DDL操作都使用Copy Table的方式完成,操作过程中原表数据库不允许写入,只能读取,在MySQL 5.5版本中引入FIC(Fast i ...

  5. json数组的解析

    一直以来,经常会遇到json数据从前端或者我经常从网站上爬取的数据中会有json数据的存在,这样如果想要获取json数据就需要对json数据进行解析 在开发过程中,经常需要和别的系统交换数据,数据交换 ...

  6. vue客户端渲染首屏优化之道

    提取第三方库,缓存,减少打包体积 1. dll动态链接库, 使用DllPlugin DllReferencePlugin,将第三方库提取出来另外打包出来,然后动态引入html.可以提高打包速度和缓存第 ...

  7. [Leetcode]724. Find Pivot Index

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  8. 如何给列表降维?sum()函数的妙用

    上个月,学习群里的 S 同学问了个题目,大意可理解为列表降维 ,例子如下: oldlist = [[1, 2, 3], [4, 5]] # 想得到结果:newlist = [1, 2, 3, 4, 5 ...

  9. Python爬虫入门教程 37-100 云沃客项目外包网数据爬虫 scrapy

    爬前叨叨 2019年开始了,今年计划写一整年的博客呢~,第一篇博客写一下 一个外包网站的爬虫,万一你从这个外包网站弄点外快呢,呵呵哒 数据分析 官方网址为 https://www.clouderwor ...

  10. Spring Cloud Alibaba基础教程:Nacos的集群部署

    继续说说生产环境的Nacos搭建,通过上一篇<Spring Cloud Alibaba基础教程:Nacos的数据持久化>的介绍,我们已经知道Nacos对配置信息的存储原理,在集群搭建的时候 ...