mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 01:08:26 -04:00
46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using NadekoBot.Core.Services;
|
|
using NadekoBot.Extensions;
|
|
using Serilog;
|
|
using StackExchange.Redis;
|
|
|
|
namespace NadekoBot.Core.Common
|
|
{
|
|
public sealed class RedisPubSub : IPubSub
|
|
{
|
|
private readonly ConnectionMultiplexer _multi;
|
|
private readonly ISeria _serializer;
|
|
private readonly IBotCredentials _creds;
|
|
|
|
public RedisPubSub(ConnectionMultiplexer multi, ISeria serializer, IBotCredentials creds)
|
|
{
|
|
_multi = multi;
|
|
_serializer = serializer;
|
|
_creds = creds;
|
|
}
|
|
|
|
public Task Pub<TData>(in TypedKey<TData> key, TData data)
|
|
{
|
|
var serialized = _serializer.Serialize(data);
|
|
return _multi.GetSubscriber().PublishAsync($"{_creds.RedisKey()}:{key.Key}", serialized, CommandFlags.FireAndForget);
|
|
}
|
|
|
|
public Task Sub<TData>(in TypedKey<TData> key, Func<TData, ValueTask> action)
|
|
{
|
|
var eventName = key.Key;
|
|
return _multi.GetSubscriber().SubscribeAsync($"{_creds.RedisKey()}:{eventName}", async (ch, data) =>
|
|
{
|
|
try
|
|
{
|
|
var dataObj = _serializer.Deserialize<TData>(data);
|
|
await action(dataObj);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error($"Error handling the event {eventName}: {ex.Message}");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
} |