37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
|
|
namespace BlazorCanvas.Server.Components.Data;
|
|
|
|
// https://developer.redis.com/develop/dotnet/streams/stream-basics/
|
|
|
|
public class RedisService: IRedisService {
|
|
private IConnectionMultiplexer _cache;
|
|
private ChannelMessageQueue _channel;
|
|
|
|
public RedisService(IConnectionMultiplexer cache) {
|
|
_cache = cache;
|
|
_channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Version Pub/Sub
|
|
/// </summary>
|
|
public async Task<CanvasCommand?> Subscribe(CancellationToken cToken) {
|
|
var mess = await _channel.ReadAsync(cToken);
|
|
var comm = JsonConvert.DeserializeObject<CanvasCommand>(mess.Message);
|
|
if (comm is not null)
|
|
return comm;
|
|
else return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Version Pub/Sub
|
|
/// </summary>
|
|
/// <param name="command"></param>
|
|
public async void Publish(CanvasCommand command) {
|
|
CanvasCommand cm = new(command);
|
|
await _cache.GetSubscriber().PublishAsync(_channel.Channel, JsonConvert.SerializeObject(cm));
|
|
}
|
|
}
|