这篇文章给大家分享的是有关SpringBoot如何解决Redis缓存+MySQL批量入库问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

架构设计

架构图:

时序图

记录基础数据MySQL表结构

CREATETABLE`zh_article_count`(`id`bigint(20)NOTNULLAUTO_INCREMENT,`bu_no`varchar(32)DEFAULTNULLCOMMENT'业务编码',`customer_id`varchar(32)DEFAULTNULLCOMMENT'用户编码',`type`int(2)DEFAULT'0'COMMENT'统计类型:0APP内文章阅读',`article_no`varchar(32)DEFAULTNULLCOMMENT'文章编码',`read_time`datetimeDEFAULTNULLCOMMENT'阅读时间',`create_time`datetimeDEFAULTCURRENT_TIMESTAMPCOMMENT'创建时间',`update_time`datetimeDEFAULTCURRENT_TIMESTAMPCOMMENT'更新时间',`param1`int(2)DEFAULTNULLCOMMENT'预留字段1',`param2`int(4)DEFAULTNULLCOMMENT'预留字段2',`param3`int(11)DEFAULTNULLCOMMENT'预留字段3',`param4`varchar(20)DEFAULTNULLCOMMENT'预留字段4',`param5`varchar(32)DEFAULTNULLCOMMENT'预留字段5',`param6`varchar(64)DEFAULTNULLCOMMENT'预留字段6',PRIMARYKEY(`id`)USINGBTREE,UNIQUEKEY`uk_zh_article_count_buno`(`bu_no`),KEY`key_zh_article_count_csign`(`customer_id`),KEY`key_zh_article_count_ano`(`article_no`),KEY`key_zh_article_count_rtime`(`read_time`))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4COMMENT='文章阅读统计表';

技术实现方案

SpringBoot

Redis

MySQL

代码实现

完整代码(GitHub,欢迎大家Star,Fork,Watch)

https://github.com/dangnianchuntian/springboot

主要代码展示

Controller

/**Copyright(c)2020.zhanghan_java@163.comAllRightsReserved.*项目名称:SpringBoot实战解决高并发数据入库:Redis缓存+MySQL批量入库*类名称:ArticleCountController.java*创建人:张晗*联系方式:zhanghan_java@163.com*开源地址:https://github.com/dangnianchuntian/springboot*博客地址:https://zhanghan.blog.csdn.net*/packagecom.zhanghan.zhredistodb.controller;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.validation.annotation.Validated;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importcom.zhanghan.zhredistodb.controller.request.PostArticleViewsRequest;importcom.zhanghan.zhredistodb.service.ArticleCountService;@RestControllerpublicclassArticleCountController{@AutowiredprivateArticleCountServicearticleCountService;/***记录用户访问记录*/@RequestMapping(value="/post/article/views",method=RequestMethod.POST)publicObjectpostArticleViews(@RequestBody@ValidatedPostArticleViewsRequestpostArticleViewsRequest){returnarticleCountService.postArticleViews(postArticleViewsRequest);}/***批量将缓存中的数据同步到MySQL(模拟定时任务操作)*/@RequestMapping(value="/post/batch",method=RequestMethod.POST)publicObjectpostBatch(){returnarticleCountService.postBatchRedisToDb();}

Service

/**Copyright(c)2020.zhanghan_java@163.comAllRightsReserved.*项目名称:SpringBoot实战解决高并发数据入库:Redis缓存+MySQL批量入库*类名称:ArticleCountServiceImpl.java*创建人:张晗*联系方式:zhanghan_java@163.com*开源地址:https://github.com/dangnianchuntian/springboot*博客地址:https://zhanghan.blog.csdn.net*/packagecom.zhanghan.zhredistodb.service.impl;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.stream.Collectors;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.stereotype.Service;importorg.springframework.util.CollectionUtils;importcom.alibaba.fastjson.JSON;importcom.zhanghan.zhredistodb.controller.request.PostArticleViewsRequest;importcom.zhanghan.zhredistodb.dto.ArticleCountDto;importcom.zhanghan.zhredistodb.mybatis.mapper.XArticleCountMapper;importcom.zhanghan.zhredistodb.service.ArticleCountService;importcom.zhanghan.zhredistodb.util.wrapper.WrapMapper;importcn.hutool.core.util.IdUtil;@ServicepublicclassArticleCountServiceImplimplementsArticleCountService{privatestaticLoggerlogger=LoggerFactory.getLogger(ArticleCountServiceImpl.class);@AutowiredprivateRedisTemplate<String,String>strRedisTemplate;privateXArticleCountMapperxArticleCountMapper;@Value("${zh.article.count.redis.key:zh}")privateStringzhArticleCountRedisKey;@Value("#{T(java.lang.Integer).parseInt('${zh..article.read.num:3}')}")privateIntegerarticleReadNum;/***记录用户访问记录*/@OverridepublicObjectpostArticleViews(PostArticleViewsRequestpostArticleViewsRequest){ArticleCountDtoarticleCountDto=newArticleCountDto();articleCountDto.setBuNo(IdUtil.simpleUUID());articleCountDto.setCustomerId(postArticleViewsRequest.getCustomerId());articleCountDto.setArticleNo(postArticleViewsRequest.getArticleNo());articleCountDto.setReadTime(newDate());StringstrArticleCountDto=JSON.toJSONString(articleCountDto);strRedisTemplate.opsForList().rightPush(zhArticleCountRedisKey,strArticleCountDto);returnWrapMapper.ok();}*批量将缓存中的数据同步到MySQLpublicObjectpostBatchRedisToDb(){Datenow=newDate();while(true){List<String>strArticleCountList=strRedisTemplate.opsForList().range(zhArticleCountRedisKey,0,articleReadNum);if(CollectionUtils.isEmpty(strArticleCountList)){returnWrapMapper.ok();}List<ArticleCountDto>articleCountDtoList=newArrayList<>();strArticleCountList.stream().forEach(x->{ArticleCountDtoarticleCountDto=JSON.parseObject(x,ArticleCountDto.class);articleCountDtoList.add(articleCountDto);});//过滤出本次定时任务之前的缓存中数据,防止死循环List<ArticleCountDto>beforeArticleCountDtoList=articleCountDtoList.stream().filter(x->x.getReadTime().before(now)).collect(Collectors.toList());if(CollectionUtils.isEmpty(beforeArticleCountDtoList)){xArticleCountMapper.batchAdd(beforeArticleCountDtoList);IntegerdelSize=beforeArticleCountDtoList.size();strRedisTemplate.opsForList().trim(zhArticleCountRedisKey,delSize,-1L);}}测试

模拟用户请求访问后台(多次请求)

查看缓存中访问数据

模拟定时任务将缓存中数据同步到DB中

这时查看缓存中的数据已经没了

查看数据库表结构

感谢各位的阅读!关于“SpringBoot如何解决Redis缓存+MySQL批量入库问题”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!