数据查询顺序
一级缓存:本地缓存 -》二级缓存:redis缓存 -》数据库
本地缓存和分布式缓存
本地缓存
:基于jvm, 意思是程序放在哪,数据就存储在哪,不需要网络请求,特别快,但是需要占用jvm的内存,所以存储的东西不能太大,也不能太久,有内存淘汰机制。缓存组件包括:Guava Cache
、Caffeine
、Encache
分布式缓存
:请求需要网络传输,速度慢一点,组件包括:Redis
,MongoDB
哪些场景用到缓存?
- 邀请入会的助力排行榜,key= 活动id+pageSize,缓存过期时间1分钟
/**
* 查询邀请入会的助力排行榜
* @param activityId
* @param pageSize
* @return
*/
@Override
@MallCache(value = "queryHelpRankingList", expireTime = 1, timeUnit = TimeUnitEnum.MINUTES)
public List<ActivityHelpStatisticsEntity> queryRankingList(String activityId, Integer pageSize) {
List<ActivityHelpStatisticsLogPO> statisticsLogs = mapper.queryRankingList(activityId, pageSize);
if (CollectionUtils.isEmpty(statisticsLogs)) {
return new ArrayList<>();
}
return statisticsLogs.stream().map(po -> ActivityHelpStatisticsConverter.convert(po))
.collect(Collectors.toList());
}
- 验证用户是否在分组中,可以=groupId, String nascentId, String groupIds,缓存五秒
@MallCache(value = "checkExistGroup", expireTime = 5, timeUnit = TimeUnitEnum.SECONDS)
private boolean checkExistGroupCache(long groupId, String nascentId, String groupIds){
return OpenPlatformHelperV4.checkExistGroup(groupId, nascentId, groupIds);
}
- 等级体系,缓存30分钟
@MallCache(value = "GradeSystem", expireTime = 30)
@Override
public GradeSystem queryGradeSystem(Long groupId, Long viewId, Long shopId) {
GradeSystemGetRequest gradeSystemGetRequest = new GradeSystemGetRequest();
gradeSystemGetRequest.setViewId(viewId);
gradeSystemGetRequest.setShopId(shopId);
gradeSystemGetRequest.setGroupId(groupId);
GradeSystemGetResponse gradeResponse = OpenPlatformClient.exec(groupId, gradeSystemGetRequest);
GradeSystem result = gradeResponse.getResult();
return result;
}
- 等级名称 30分钟,这种需要调用三方接口,缓存可以减少调用接口次数
/**
* 获取等级名称
*
* @param groupId
* @param shopId
* @param grade
* @return
*/
@MallCache(value = "CustomerGradeName", expireTime = 30)
public String getCustomerGradeName(long groupId, long shopId, int grade) {
List<Long> viewIds = cloudPlatformService.queryViewIdByAreaIdOrBrandId(groupId, getMallId(), Arrays.asList(shopId));
return getCustomerGradeName(groupId, shopId, grade, viewIds.get(0));
}
public String getCustomerGradeName(long groupId, long shopId, int grade, long viewId) {
if (grade == 0) {
return "非会员";
}
GradeInfoGetResponse response = OpenPlatformHelperV4.getGradeInfo(groupId, shopId, grade, viewId);
AssertUtil.assertTrue(response != null && response.getSuccess(), "获取等级信息失败");
GradeInfo gradeInfo = response.getResult();
AssertUtil.assertNotNull(gradeInfo, "获取等级信息失败");
return gradeInfo.getGradeName();
}
@Override
@MallCache(value = "VIEWID", expireTime = 60)
public Long queryViewIdByAreaIdOrBrandId(Long groupId, Long mallId, Long shopId) {
WmCompanyDO company = WmCompanyDao.dao().findByCompanyId(groupId);
List<Long> viewIds = CloudPlatformHelper.queryViewIdByShopIds(Arrays.asList(shopId), groupId, company.getViewOperationType());
AssertUtil.assertTrue(!CollectionUtils.isEmpty(viewIds), "查询运营视角失败,请检查");
return viewIds.get(0);
}
- 线下门店列表 缓存60分钟,这种东西一般不会经常变化,且进入首页就要用,
/**
*
* @Description 获取线下门店列表,并存入缓存
* @Param
* @param groupId
* @param shopId
* @return
*/
@MallCache(value = "offlineShops", expireTime = 60)
public TableResponse<Record> getOfflineShops(Long groupId,Long shopId){
TableResponse<Record> tableResponse = new TableResponse<>();
List<Record> offlineShops = WmOfflineShopDao.dao(