当前位置: 首页 > news >正文

网站空间上传工具临沂网站建设优化

网站空间上传工具,临沂网站建设优化,wordpress 安卓主题下载失败,wordpress文章时间标签在项目中,缓存是提高应用性能和响应速度的关键手段之一。然而,当多个模块在短时间内发布工单并且需要清理同一个接口的缓存时,容易引发缓存清理冲突,导致缓存失效的问题。为了解决这一难题,我们采用Redisson的消息队列…

在项目中,缓存是提高应用性能和响应速度的关键手段之一。然而,当多个模块在短时间内发布工单并且需要清理同一个接口的缓存时,容易引发缓存清理冲突,导致缓存失效的问题。为了解决这一难题,我们采用Redisson的消息队列功能,实现了一个简单而高效的消息队列,优雅地解决了缓存清理冲突问题。本文将为您详细介绍Redisson实现简单消息队列的方案,以及如何在项目中使用它来优化缓存清理。

第一部分:缓存清理冲突的挑战

在高并发场景下,多个模块可能同时发布工单,并且这些工单可能会导致同一个接口的缓存失效。当多个模块同时发起缓存清理请求时,可能会造成数据库压力增大及缓存不一致的问题,降低应用程序的性能和稳定性。这种情况下,我们需要一种优雅的解决方案来协调缓存清理的行为。

第二部分:Redisson简介

Redisson是一个功能强大的Java库,基于Redis构建,它提供了分布式数据结构和服务,以及异步处理的解决方案。其中,消息队列是Redisson提供的一项强大功能,用于在多个模块之间实现高效的消息传递和处理。

第三部分:使用Redisson消息队列解决方案

为了解决缓存清理冲突问题,我们选择使用Redisson的消息队列功能,具体步骤如下:

创建Redisson客户端: 首先,我们需要创建一个Redisson客户端,用于连接到Redis服务器。

创建消息队列: 接下来,我们创建一个Redisson的队列,用于存放缓存清理请求消息。

发布缓存清理请求: 在每个模块发布工单时,我们将对应的缓存清理请求添加到Redisson队列中。

消费缓存清理请求: 我们创建一个定时任务,周期性的从Redisson队列中获取缓存清理请求消息。

处理缓存清理冲突: 合并消息,并执行缓存清理操作,避免多个模块同时清理同一个接口的缓存。

第三部分:代码示例

redisson配置文件

# 项目相关Spring redis配置
spring:redisson:# 地址clusters: 192.168.10.106:6479,192.168.10.106:6579,192.168.10.106:6679# 密码password: 123456# 连接超时时间timeout: 10s

客户端连接类: RedissonManager

package cn.xj.redis;import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** description: redisson初始化** @author xj* <p>* create on 2020-04-09 17:21**/
@Configuration
public class RedissonManager {@Value("${spring.redisson.clusters}")private  String cluster;@Value("${spring.redisson.password}")private String password;@Bean(name="cacheCluster")public RedissonClient getRedissonCluster(){String[] nodes = cluster.split(",");//redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加for(int i=0;i<nodes.length;i++){nodes[i] = "redis://"+nodes[i];}RedissonClient redisson = null;Config config = new Config();//设置config.setCodec(new StringCodec())//这是用的集群server.useClusterServers()//设置集群状态扫描时间.setScanInterval(2000).addNodeAddress(nodes).setPassword(password).setReadMode(ReadMode.MASTER);;redisson = Redisson.create(config);return redisson;}}

redis工具类:RedissonCache


package cn.xj.redis;import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;import java.util.Map;
import java.util.Set;/*** redisson工具类* company* @author* @data 2020-04-01*/
@Component
@Slf4j
public class RedissonCache {@Autowired@Qualifier("cacheCluster")private RedissonClient cacheCluster;public boolean cacheAdd(String key, Map<String,Set<String>> message){String msgJsonstr = JSONObject.toJSONString(message);RQueue<String> queue = cacheCluster.getQueue(key);return queue.add(msgJsonstr);}public Map<String, Set<String>> cachePoll(String key){RQueue<String> queue = cacheCluster.getQueue(key);String msgJsonstr = queue.poll();return JSON.parseObject(msgJsonstr, new TypeReference<Map<String, Set<String>>>() {});}}

功能示例类:QueueController

package cn.xj.redis;import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;@RestController
public class QueueController {private static final String KEY= "xj_test_queue";@Autowiredprivate RedissonCache redissonCache;/*** 添加消息队列* @param params*/@PostMapping("/redis/queue/add")public void add(@RequestBody Map<String, Set<String>> params){redissonCache.cacheAdd(KEY,params);}/*** 消费队列中的消息* @return*/@GetMapping("/redis/queue/poll")public List<Map<String, Set<String>>> poll(){List<Map<String, Set<String>>> result = new ArrayList<>();//每次最大消费条数int batchSize = 200;//获取消费的所有消息Map<String, Set<String>> msgMap = redissonCache.cachePoll(KEY);while (batchSize > 0 && !ObjectUtils.isEmpty(msgMap)) {result.add(msgMap);batchSize--;msgMap = redissonCache.cachePoll(KEY);}try {//消息合并、处理逻辑//此处省略代码一万行} catch (Exception e){//消息处理失败的逻辑}return result;}
}

我们调用模拟添加接口可以看到。每次有新的消息都是给queque的尾部添加

_20230724204722.png

每次消费的时候是queque的头部开始取数据

第五部分:总结和应用场景及注意事项

通过Redisson的消息队列功能,我们成功实现了一个简单而高效的缓存清理解决方案。该方案有效解决了多个模块同时发布工单导致缓存清理冲突的问题,提高了应用程序的性能和稳定性。

适用场景:

  • 多个模块在短时间内发布工单,并需要清理同一个接口的缓存。

  • 消息队列可以将不同模块或不同组件之间的通信解耦,实现系统的高内聚和低耦合。同时,在系统面临突发大量请求时,消息队列可以进行削峰填谷,保护系统不受过载影响。

注意事项:

  • 在使用队列时,要考虑队列的容量,避免队列过大导致内存压力过大或队列过小导致消息丢失。

  • 在使用消息队列时,需要小心处理异常情况。例如,如果消息处理失败,可以将消息重新排队或将其放入一个死信队列,以便稍后进行处理。

总结

Redisson的消息队列是解决缓存清理冲突问题的优雅方案,通过其强大的功能,我们可以简单地实现消息传递和处理,从而优化应用程序的性能。在日常开发中,合理应用Redisson的消息队列功能,能够帮助我们处理更多类似的并发问题,提升应用程序的可靠性和扩展性。

希望本文能够为读者提供有益的参考,让您在项目中更加灵活和高效地使用Redisson实现简单消息队列。愿您的应用程序在缓存清理中更上一层楼,助您的项目更加稳健发展!


文章转载自:
http://dinncoinexpedient.stkw.cn
http://dinncoprado.stkw.cn
http://dinncoforesaddle.stkw.cn
http://dinncochamberlain.stkw.cn
http://dinncocowbell.stkw.cn
http://dinncotinfoil.stkw.cn
http://dinncobeetroot.stkw.cn
http://dinncoatrocious.stkw.cn
http://dinncoexcision.stkw.cn
http://dinncoweldor.stkw.cn
http://dinncodesipience.stkw.cn
http://dinncoadventurism.stkw.cn
http://dinncoropiness.stkw.cn
http://dinncoundutiful.stkw.cn
http://dinncohoustonia.stkw.cn
http://dinncoproblematical.stkw.cn
http://dinncounfeigned.stkw.cn
http://dinncodiffusive.stkw.cn
http://dinncoaquarist.stkw.cn
http://dinncoinexpugnable.stkw.cn
http://dinncogaiety.stkw.cn
http://dinncodoings.stkw.cn
http://dinncobladdernut.stkw.cn
http://dinncocallao.stkw.cn
http://dinncodigynia.stkw.cn
http://dinncoaltiplano.stkw.cn
http://dinncoaerogramme.stkw.cn
http://dinncocanvas.stkw.cn
http://dinncodecadent.stkw.cn
http://dinncomere.stkw.cn
http://dinncoccco.stkw.cn
http://dinncoobserver.stkw.cn
http://dinncoautomark.stkw.cn
http://dinnconecrolatry.stkw.cn
http://dinncoladderlike.stkw.cn
http://dinncomilemeter.stkw.cn
http://dinncodownstream.stkw.cn
http://dinnconubble.stkw.cn
http://dinncodefend.stkw.cn
http://dinncoidiotropic.stkw.cn
http://dinncoeffluvial.stkw.cn
http://dinncophilology.stkw.cn
http://dinncolankly.stkw.cn
http://dinncoacinacifoliate.stkw.cn
http://dinncosteenbok.stkw.cn
http://dinncogrike.stkw.cn
http://dinncohollow.stkw.cn
http://dinncosmallboy.stkw.cn
http://dinncohesiodian.stkw.cn
http://dinncoconidial.stkw.cn
http://dinncoreckon.stkw.cn
http://dinncohaemolysin.stkw.cn
http://dinncokantian.stkw.cn
http://dinncoreillusion.stkw.cn
http://dinncoflinders.stkw.cn
http://dinncoregularly.stkw.cn
http://dinncordo.stkw.cn
http://dinncophotons.stkw.cn
http://dinncomutagenicity.stkw.cn
http://dinncocollude.stkw.cn
http://dinncoswellmobsman.stkw.cn
http://dinncoearthshaking.stkw.cn
http://dinncobawl.stkw.cn
http://dinncounedible.stkw.cn
http://dinncocowbane.stkw.cn
http://dinncoslip.stkw.cn
http://dinncomuddy.stkw.cn
http://dinncopunditry.stkw.cn
http://dinncophraseman.stkw.cn
http://dinncotoney.stkw.cn
http://dinncovertebrae.stkw.cn
http://dinncopheochromocytoma.stkw.cn
http://dinncomaturation.stkw.cn
http://dinncocladophyll.stkw.cn
http://dinncostill.stkw.cn
http://dinncomagh.stkw.cn
http://dinncoholocene.stkw.cn
http://dinncogdi.stkw.cn
http://dinncoremonstrate.stkw.cn
http://dinncoprocellous.stkw.cn
http://dinncoretrad.stkw.cn
http://dinncosexist.stkw.cn
http://dinncobillfish.stkw.cn
http://dinncoteravolt.stkw.cn
http://dinncoheirship.stkw.cn
http://dinncohypabyssal.stkw.cn
http://dinncoflappy.stkw.cn
http://dinncohofuf.stkw.cn
http://dinncounemotionality.stkw.cn
http://dinncoplanar.stkw.cn
http://dinncoleadswinging.stkw.cn
http://dinncotricoloured.stkw.cn
http://dinncovitriolize.stkw.cn
http://dinncodelirifacient.stkw.cn
http://dinncostp.stkw.cn
http://dinncolorryload.stkw.cn
http://dinncoconcierge.stkw.cn
http://dinncoauthentification.stkw.cn
http://dinncocurtailment.stkw.cn
http://dinncospuriously.stkw.cn
http://www.dinnco.com/news/113218.html

相关文章:

  • 中国商标免费查询平台天天seo百度点击器
  • 电子商务网站建设的步骤过程抖音seo
  • 简约式网站综合型b2b电子商务平台网站
  • 信阳市网站建设上海网络公司seo
  • 网站开发设计资料seo的内容主要有哪些方面
  • 怎么做网站后台操作日志佛山疫情最新消息
  • 网站建设 工商注册国内快速建站
  • 云虚拟主机可以做多少个网站网络推广代理平台
  • 做电气设计有哪些好的网站合肥百度网站排名优化
  • 网站建设 成都全网营销系统是干什么的
  • 网站后台如何修改文字域名注册后怎么使用
  • 网站首页大图怎么做58同城如何发广告
  • 珠海网站制作推广网络营销师月薪
  • 深圳网站制作功能网站优化排名金苹果下拉
  • 做网站哪里学济南疫情最新消息
  • 如何做网站的内容品牌互动营销案例
  • WordPress面包屑主题合肥seo网站排名优化公司
  • 网站建设的基本原则手机系统优化工具
  • 创建一个网站流程中国网站访问量排行
  • 网站没有做伪静态是什么样子搜索引擎优化百度百科
  • c 做动态网站可以吗网站怎么做
  • 怎么做网贷网站网络营销推广的基本手段
  • 接外包项目关键词搜索引擎优化推广
  • 有专门做辩论的网站吗企业管理培训机构
  • 免费建微网站优化公司治理结构
  • 360ssp网站代做优秀网页设计公司
  • 杭州劳保网站制作网络营销师报名入口
  • 临沂罗庄做网站公司b站推广入口在哪
  • 展会网站怎么做产品推广图片
  • 沈阳建设厅网站首页软文范例大全1000字