// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using NewLife.Caching.Queues;
namespace Admin.NET.Core;
///
/// Redis 消息队列
///
public static class RedisQueue
{
private static ICache _cache = App.GetService();
///
/// 获取可信队列,需要确认
///
///
///
///
public static RedisReliableQueue GetRedisReliableQueue(string topic)
{
var queue = (_cache as FullRedis).GetReliableQueue(topic);
return queue;
}
///
/// 可信队列回滚
///
///
///
///
public static int RollbackAllAck(string topic, int retryInterval = 60)
{
var queue = GetRedisReliableQueue(topic);
queue.RetryInterval = retryInterval;
return queue.RollbackAllAck();
}
///
/// 发送一个数据列表到可信队列
///
///
///
///
///
public static int AddReliableQueueList(string topic, List value)
{
var queue = (_cache as FullRedis).GetReliableQueue(topic);
var count = queue.Count;
var result = queue.Add(value.ToArray());
return result - count;
}
///
/// 发送一条数据到可信队列
///
///
///
///
///
public static int AddReliableQueue(string topic, T value)
{
var queue = (_cache as FullRedis).GetReliableQueue(topic);
var count = queue.Count;
var result = queue.Add(value);
return result - count;
}
///
/// 获取延迟队列
///
///
///
///
public static RedisDelayQueue GetDelayQueue(string topic)
{
var queue = (_cache as FullRedis).GetDelayQueue(topic);
return queue;
}
///
/// 发送一条数据到延迟队列
///
///
///
/// 延迟时间。单位秒
///
///
public static int AddDelayQueue(string topic, T value, int delay)
{
var queue = GetDelayQueue(topic);
return queue.Add(value, delay);
}
///
/// 发送数据列表到延迟队列
///
///
///
///
/// 延迟时间。单位秒
///
public static int AddDelayQueue(string topic, List value, int delay)
{
var queue = GetDelayQueue(topic);
queue.Delay = delay;
return queue.Add(value.ToArray());
}
///
/// 在可信队列获取一条数据
///
///
///
///
public static T ReliableTakeOne(string topic)
{
var queue = GetRedisReliableQueue(topic);
return queue.TakeOne(1);
}
///
/// 异步在可信队列获取一条数据
///
///
///
///
public static async Task ReliableTakeOneAsync(string topic)
{
var queue = GetRedisReliableQueue(topic);
return await queue.TakeOneAsync(1);
}
///
/// 在可信队列获取多条数据
///
///
///
///
///
public static List ReliableTake(string topic, int count)
{
var queue = GetRedisReliableQueue(topic);
return queue.Take(count).ToList();
}
}