springboot~hazelcast缓存中间件
缓存来了
在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缓存中间件的更多相关文章
- springboot与缓存(redis,或者caffeine,guava)
1.理论介绍 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvide ...
- 学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用
学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用 一.SpringBoot系列教程 二.SpringBoot ...
- .net 分布式架构之分布式缓存中间件
开源git地址: http://git.oschina.net/chejiangyi/XXF.BaseService.DistributedCache 分布式缓存中间件 方便实现缓存的分布式,集群, ...
- 带着新人学springboot的应用03(springboot+mybatis+缓存 下)
springboot+mybatis+缓存,基本的用法想必是会了,现在说一说内部大概的原理. 稍微提一下mybatis,只要导入了mybatis的依赖,那么有个自动配置类就会生效,你可以去mybati ...
- redis缓存中间件基础
前序: 默认使用SimpleCacheConfiguration 组件ConcurrentMapCacheManager==ConcurrentMapCache将数据保存在ConcurrentMap& ...
- SpringBoot 与缓存
1. JSR107 Java Caching 定义了5个核心接口: CachingProvider:定义了创建,配置,获取,管理和控制多个CacheManager; CacheManager:定义了创 ...
- SpringBoot与缓存
一.Spring Boot与缓存. JSR-107.Spring缓存抽象.整合Redis 一.JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, Cach ...
- 老司机带你玩转面试(1):缓存中间件 Redis 基础知识以及数据持久化
引言 今天周末,我在家坐着掐指一算,马上又要到一年一度的金九银十招聘季了,国内今年上半年受到 YQ 冲击,金三银四泡汤了,这就直接导致很多今年毕业的同学会和明年毕业的同学一起参加今年下半年的秋招,这个 ...
- SpringBoot 整合缓存Cacheable实战详细使用
前言 我知道在接口api项目中,频繁的调用接口获取数据,查询数据库是非常耗费资源的,于是就有了缓存技术,可以把一些不常更新,或者经常使用的数据,缓存起来,然后下次再请求时候,就直接从缓存中获取,不需要 ...
随机推荐
- bzoj3534 [Sdoi2014]重建
变形的$Martix-Tree$定理 发现我们要求的是$\prod_{i \in E}{p_{i}} * \prod_{i \notin E}{(1-p_{i})}$ 然后呢? 矩阵树对重边也有效对吧 ...
- POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置关系
POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置 Description Calculate the number of toys th ...
- iOS 社交化分享功能
iOS 开发过程中可能会遇到需要进行第三方分享的需求,比如向QQ,微信,微博等分享 如下图 我们今天要讲到的方式是使用了一个第三方工具: http://www.sharesdk.cn 一,注册账号 去 ...
- 在CentOS下安装Git
1.安装git依赖包 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUti ...
- 在linux服务器之间复制文件和目录命令scp
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...
- PCB泪滴设计
操作:选择[Tools]-->[Teardrops],快捷键T+E.打开[Teardrop Options]对话框进行设置.如下图所示: 对话框面板介绍 [General] 1.该区域的[Pad ...
- python书籍推荐:python编码推荐(高清完整pdf)
目录INF-qa Python 编码规范................................................................................ ...
- 安卓开发笔记(三十一):shape标签下子类根结点的具体使用
在我的上一篇博文当中阐述了我们如何使用shape标签进行自定义控件,这里对shape控件的属性进行阐述,不知道如何使用这些属性的可以参见我的上一篇博文(自定义Button):https://www.c ...
- zookeeper源码 — 二、集群启动—leader选举
上一篇介绍了zookeeper的单机启动,集群模式下启动和单机启动有相似的地方,但是也有各自的特点.集群模式的配置方式和单机模式也是不一样的,这一篇主要包含以下内容: 概念介绍:角色,服务器状态 服务 ...
- 执行Python程序的两种方式
目录 交互式(了解) 命令行式(了解) Python执行程序的三个阶段(掌握) 交互式(了解) 交互式环境下,敲完一条命令按下enter键马上能看到结果,调试程序方便.程序无法永久保存,关掉cmd窗口 ...