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

Vue3前端实现一个本地消息队列(MQ), 让消息延迟消费或者做缓存

MQ功能实现的具体代码(TsMQ.ts):

import { v4 as uuidx } from 'uuid';import emitter from '@/utils/mitt'// 消息类
class Message {// 过期时间,0表示马上就消费exp: number;// 消费标识,避免重复消费tag : string;// 消息体body : any;constructor( exp: number ,  body : any ) {if (exp == 0) {this.exp = 0;}else {this.exp = Date.now().valueOf() + exp;}this.body = body;this.tag = uuidx();}}export default class TsMQ {static tsMQ : TsMQ;cache : boolean;repository : Map<string, any[]>;/*设置只作为缓存使用*/setCache(cache : boolean)  {this.cache = cache;}/*获取单列的MQ对象*/static getInstance() : TsMQ {if(this.tsMQ == null) {this.tsMQ = new TsMQ();}return this.tsMQ;}constructor() {this.repository = new Map<string,any[]>();this.cache = false;setInterval(()=> {Array.from(this.repository.keys()).forEach( key => {let poll = this.repository.get(key) as any[];if(poll.length > 0) {poll.forEach( item => {if (item.exp == 0 || item.exp <= Date.now().valueOf() - 100) {// 如果是作为缓存使用就不会放入mitt就行推送if(!this.cache) {emitter.emit(key,item.body);}let single : any[] = this.repository.get(key) as any[];single = single.filter(dispose => {return dispose.tag !== item.tag;});this.repository.set(key,single);}});}});},100)}/** @describe 放消息入队列* @param queue : string 队列名称* @param exp : number 消息消费时间* @param  message : any 消息体*/pushMessage( queue : string , exp : number,  message : any ) {if(this.repository.has(queue)) {let single : any[] = this.repository.get(queue) as any[];single.push(new Message(exp,message));this.repository.set(queue,single);}else {let temp = [];temp.push(new Message(exp,message));this.repository.set(queue,temp);}}/** @describe 直接获取消息,可以配合做本地缓存,就要去掉constructor的轮询中的事件推送* @param queue : string 队列名称*/takeMessage( queue : string ) : any {let single : any[] = this.repository.get(queue) as any[];if( single && single.length > 0) {let message = single.shift();this.repository.set(queue,single);return message;}else {return '队列没有消息!';}}}
提示:其中需要用到三方的uuid和mitt,然后要消息持久化可以用到pinia来让消息持久化,本案列没有采用持久化
uuid的三方包安装命令
npm install uuid
mitt的三方安装命令 
npm install --save mitt 

 其中mitt文件的代码

import mitt from "mitt";const emitter = mitt();export default emitter;

使用方式:

投送消息
<script setup lang="ts">import TsMQ from '@/utils/TsMQ'import emitter from '@/utils/mitt'let tsMQ : TsMQ = TsMQ.getInstance();//投送消息function pushMQs() {console.log(`M2投递时间:${new Date().toLocaleString()}`)tsMQ.pushMessage("HelloWord",1000 * 20,{ name : 'M2', second:20 });tsMQ.pushMessage("HelloWord",1000 * 3,{ name : 'M1', second:3 });}</script><template><div id="app" style="display: flex;flex-direction: column;justify-content: center;height: 100%;width: 100%;margin: 0;padding: 0"><span @click="pushMQs">MQ投送</span></div>
</template>

提示:这里采用事件模拟投送消息 

 消息订阅代码:
<script setup lang="ts">import TsMQ from '@/utils/TsMQ'import emitter from '@/utils/mitt'let tsMQ : TsMQ = TsMQ.getInstance();//订阅消息emitter.on("HelloWord",e => {console.log(`收到消息:${JSON.stringify(e)}\n消息时间:${new Date().toLocaleString()}`)}); function takeMQs() {console.log(tsMQ.takeMessage('HelloWord'));}</script><template><div id="app" style="display: flex;flex-direction: column;justify-content: center;height: 100%;width: 100%;margin: 0;padding: 0"><span @click="takeMQs">MQ获取</span></div>
</template>  

效果:

总结:我们可以看到我们实现了这个功能 ,其中可以用来作为缓存使用,同理可写

http://www.lryc.cn/news/307366.html

相关文章:

  • 普中51单片机学习(8*8LED点阵)
  • Python 实现Excel自动化办公(上)
  • DayDreamInGIS 之 ArcGIS Pro二次开发 图层属性中换行符等特殊字符替换
  • RK3568平台 RTC时间框架
  • 番外篇 | YOLOv5+DeepSort实现行人目标跟踪检测
  • 认识Sass
  • YOLOv9-Openvino和ONNXRuntime推理【CPU】
  • AIGC 架构:RAG (retrieval augumented generation) 应用可以使用 PostgreSQL 作为向量数据库组件吗?
  • leetcode:134.加油站
  • uniapp的微信小程序授权头像昵称(最新版)
  • Spring Boot到底是如何进行自动配置的?
  • 【王道数据结构】【chapter7查找】【P285t5】
  • 个人玩航拍,如何申请无人机空域?
  • ChatGPT带火的HBM是什么?
  • 10 款数据恢复软件功能和有效性对比(2024 年更新)
  • Python 与 pdfplumber:高效自动读取 PDF 的解决方案
  • Flutter 启动流程解析
  • 全量知识系统问题及SmartChat给出的答复 之4
  • Java架构师之路七、大数据:Hadoop、Spark、Hive、HBase、Kafka等
  • 图论基础(一)
  • 使用 React 和 MUI 创建多选 Checkbox 树组件
  • vue3里面使用el-image-vie出现图片预览导致页面卡顿停止加载问题
  • Leetcoder Day26| 回溯part06:总结+三道hard题
  • 浅谈 Linux 网络编程 - 网络字节序
  • Nginx网络服务六-----IP透传、调度算法和负载均衡
  • 【Linux进程】进程状态---进程僵尸与孤儿
  • MySQL数据库基础知识总结(适合小白入门使用)一
  • 历史新知网:寄快递寄个电脑显示器要多少钱?
  • 在两台CentOS 7服务器上部署MinIO集群。
  • 【计算机网络】深度学习使用应用层的HTTP协议