You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.Extensions.Logging;
using ServiceStack.Messaging;
using ServiceStack.Redis;
using SlnMesnac.Config;
using StackExchange.Redis;
using System;
using System.Linq;
using System.Security.Cryptography;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本4.0.30319.42000
* 机器名称LAPTOP-E0N2L34V
* 命名空间SlnMesnac.Redis
* 唯一标识98350fd3-5b25-4395-a2f0-eb949cc4c6f6
*
* 创建者WenJY
* 电子邮箱wenjy@mesnac.com
* 创建时间2024-04-11 16:44:24
* 版本V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.Redis
{
/// <summary>
/// Redis
/// </summary>
public class RedisHandler
{
private ILogger<RedisHandler> _logger;
private readonly AppConfig _appConfig;
private readonly ISubscriber _subscriber;
public readonly ConnectionMultiplexer redis;
public RedisHandler(AppConfig appConfig, ILogger<RedisHandler> logger)
{
_appConfig = appConfig;
redis = ConnectionMultiplexer.Connect(_appConfig.redisConfig);
_subscriber = redis.GetSubscriber();
_logger = logger;
}
/// <summary>
/// 推送消息
/// </summary>
/// <param name="channel"></param>
/// <param name="message"></param>
public void PublishMessage(string channel, string message)
{
// 生成唯一的消息ID
// var messageId = $"{DateTime.UtcNow.Ticks}_{Guid.NewGuid().ToString("N")}";
// 存储消息内容
// var redisDb = redis.GetDatabase();
// redisDb.StringSet(messageId, message, TimeSpan.FromMinutes(10)); // 设置消息的有效期
// 发布消息ID
long res = _subscriber.Publish(channel, message);
_logger.LogInformation($"向主题:{channel};推送消息:{message};结果:{res}");
}
/// <summary>
/// 推送消息到指定工位的队列
/// </summary>
/// <param name="workerId">工位ID</param>
/// <param name="message">消息内容</param>
public void PublishMessageToWorker(int workerId, string message)
{
var channel = $"channel_worker_{workerId}";
// 生成唯一的消息ID
var messageId = $"{DateTime.UtcNow.Ticks}_{Guid.NewGuid().ToString("N")}";
// 存储消息内容,使用列表结构
var redisDb = redis.GetDatabase();
redisDb.ListRightPush(channel, message); // 将消息推入对应工位的队列
_logger.LogInformation($"向工位:{workerId} 的队列推送消息ID:{messageId},消息内容:{message}");
}
/// <summary>
/// 指定工位,从队列中获取一条消息(客户端消费时使用)
/// </summary>
/// <param name="workerId">工位ID</param>
/// <returns>消息内容如果没有消息则返回null</returns>
public string ConsumeMessageFromWorker(string workId,int dataBaseId = 0)
{
var channel = workId;
var redisDb = redis.GetDatabase(dataBaseId);
// 从队列左侧弹出消息
var message = redisDb.ListLeftPop(channel);
if (message.IsNull)
{
return null;
}
return (string)message;
}
/// <summary>
/// 确认消息
/// </summary>
/// <param name="messageId"></param>
public void AcknowledgeMessage(string messageId)
{
var redisDb = redis.GetDatabase();
redisDb.KeyDelete(messageId); // 删除确认的消息
_logger.LogInformation($"消息ID:{messageId} 已被确认处理");
}
/// <summary>
/// 订阅消息
/// </summary>
/// <param name="channel"></param>
/// <param name="onMessageReceived"></param>
public void SubscribeToChannel(string channel, Action<string, string> onMessageReceived)
{
_subscriber.Subscribe(channel, (ch, message) =>
{
onMessageReceived(ch, message);
_logger.LogInformation($"订阅主题:{channel};收到主题:{ch};推送的消息:{message}");
});
}
}
}