您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Spring Boot如何解決Redis緩存+MySQL批量入庫問題的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
架構(gòu)圖:
時(shí)序圖
記錄基礎(chǔ)數(shù)據(jù)MySQL表結(jié)構(gòu)
CREATE TABLE `zh_article_count` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `bu_no` varchar(32) DEFAULT NULL COMMENT '業(yè)務(wù)編碼', `customer_id` varchar(32) DEFAULT NULL COMMENT '用戶編碼', `type` int(2) DEFAULT '0' COMMENT '統(tǒng)計(jì)類型:0APP內(nèi)文章閱讀', `article_no` varchar(32) DEFAULT NULL COMMENT '文章編碼', `read_time` datetime DEFAULT NULL COMMENT '閱讀時(shí)間', `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新時(shí)間', `param1` int(2) DEFAULT NULL COMMENT '預(yù)留字段1', `param2` int(4) DEFAULT NULL COMMENT '預(yù)留字段2', `param3` int(11) DEFAULT NULL COMMENT '預(yù)留字段3', `param4` varchar(20) DEFAULT NULL COMMENT '預(yù)留字段4', `param5` varchar(32) DEFAULT NULL COMMENT '預(yù)留字段5', `param6` varchar(64) DEFAULT NULL COMMENT '預(yù)留字段6', PRIMARY KEY (`id`) USING BTREE, UNIQUE KEY `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=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文章閱讀統(tǒng)計(jì)表';
技術(shù)實(shí)現(xiàn)方案
SpringBoot
Redis
MySQL
完整代碼(GitHub,歡迎大家Star,Fork,Watch)
https://github.com/dangnianchuntian/springboot
主要代碼展示
Controller
/* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. * 項(xiàng)目名稱:Spring Boot實(shí)戰(zhàn)解決高并發(fā)數(shù)據(jù)入庫: Redis 緩存+MySQL 批量入庫 * 類名稱:ArticleCountController.java * 創(chuàng)建人:張晗 * 聯(lián)系方式:zhanghan_java@163.com * 開源地址: https://github.com/dangnianchuntian/springboot * 博客地址: https://zhanghan.blog.csdn.net */ package com.zhanghan.zhredistodb.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.zhanghan.zhredistodb.controller.request.PostArticleViewsRequest; import com.zhanghan.zhredistodb.service.ArticleCountService; @RestController public class ArticleCountController { @Autowired private ArticleCountService articleCountService; /** * 記錄用戶訪問記錄 */ @RequestMapping(value = "/post/article/views", method = RequestMethod.POST) public Object postArticleViews(@RequestBody @Validated PostArticleViewsRequest postArticleViewsRequest) { return articleCountService.postArticleViews(postArticleViewsRequest); } /** * 批量將緩存中的數(shù)據(jù)同步到MySQL(模擬定時(shí)任務(wù)操作) */ @RequestMapping(value = "/post/batch", method = RequestMethod.POST) public Object postBatch() { return articleCountService.postBatchRedisToDb(); }
Service
/* * Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved. * 項(xiàng)目名稱:Spring Boot實(shí)戰(zhàn)解決高并發(fā)數(shù)據(jù)入庫: Redis 緩存+MySQL 批量入庫 * 類名稱:ArticleCountServiceImpl.java * 創(chuàng)建人:張晗 * 聯(lián)系方式:zhanghan_java@163.com * 開源地址: https://github.com/dangnianchuntian/springboot * 博客地址: https://zhanghan.blog.csdn.net */ package com.zhanghan.zhredistodb.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import com.alibaba.fastjson.JSON; import com.zhanghan.zhredistodb.controller.request.PostArticleViewsRequest; import com.zhanghan.zhredistodb.dto.ArticleCountDto; import com.zhanghan.zhredistodb.mybatis.mapper.XArticleCountMapper; import com.zhanghan.zhredistodb.service.ArticleCountService; import com.zhanghan.zhredistodb.util.wrapper.WrapMapper; import cn.hutool.core.util.IdUtil; @Service public class ArticleCountServiceImpl implements ArticleCountService { private static Logger logger = LoggerFactory.getLogger(ArticleCountServiceImpl.class); @Autowired private RedisTemplate<String, String> strRedisTemplate; private XArticleCountMapper xArticleCountMapper; @Value("${zh.article.count.redis.key:zh}") private String zhArticleCountRedisKey; @Value("#{T(java.lang.Integer).parseInt('${zh..article.read.num:3}')}") private Integer articleReadNum; /** * 記錄用戶訪問記錄 */ @Override public Object postArticleViews(PostArticleViewsRequest postArticleViewsRequest) { ArticleCountDto articleCountDto = new ArticleCountDto(); articleCountDto.setBuNo(IdUtil.simpleUUID()); articleCountDto.setCustomerId(postArticleViewsRequest.getCustomerId()); articleCountDto.setArticleNo(postArticleViewsRequest.getArticleNo()); articleCountDto.setReadTime(new Date()); String strArticleCountDto = JSON.toJSONString(articleCountDto); strRedisTemplate.opsForList().rightPush(zhArticleCountRedisKey, strArticleCountDto); return WrapMapper.ok(); } * 批量將緩存中的數(shù)據(jù)同步到MySQL public Object postBatchRedisToDb() { Date now = new Date(); while (true) { List<String> strArticleCountList = strRedisTemplate.opsForList().range(zhArticleCountRedisKey, 0, articleReadNum); if (CollectionUtils.isEmpty(strArticleCountList)) { return WrapMapper.ok(); } List<ArticleCountDto> articleCountDtoList = new ArrayList<>(); strArticleCountList.stream().forEach(x -> { ArticleCountDto articleCountDto = JSON.parseObject(x, ArticleCountDto.class); articleCountDtoList.add(articleCountDto); }); //過濾出本次定時(shí)任務(wù)之前的緩存中數(shù)據(jù),防止死循環(huán) List<ArticleCountDto> beforeArticleCountDtoList = articleCountDtoList.stream().filter(x -> x.getReadTime() .before(now)).collect(Collectors.toList()); if (CollectionUtils.isEmpty(beforeArticleCountDtoList)) { xArticleCountMapper.batchAdd(beforeArticleCountDtoList); Integer delSize = beforeArticleCountDtoList.size(); strRedisTemplate.opsForList().trim(zhArticleCountRedisKey, delSize, -1L); } }
模擬用戶請(qǐng)求訪問后臺(tái)(多次請(qǐng)求)
查看緩存中訪問數(shù)據(jù)
模擬定時(shí)任務(wù)將緩存中數(shù)據(jù)同步到DB中
這時(shí)查看緩存中的數(shù)據(jù)已經(jīng)沒了
查看數(shù)據(jù)庫表結(jié)構(gòu)
感謝各位的閱讀!關(guān)于“Spring Boot如何解決Redis緩存+MySQL批量入庫問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。