OH YEAH.
This commit is contained in:
@@ -9,9 +9,11 @@ namespace BlazorCanvas.Server.Components.Data;
|
||||
// https://developer.redis.com/develop/dotnet/streams/stream-basics/
|
||||
|
||||
public class RedisService : IRedisService {
|
||||
const string STREAM_NAME = "steamie",
|
||||
GROUP_NAME = "groupie",
|
||||
SUB_NAME = "servant";
|
||||
private const string STREAM_NAME = "steamie",
|
||||
GROUP_NAME = "groupie",
|
||||
SUB_NAME = "servant";
|
||||
|
||||
private string _lastId = "0-0";
|
||||
|
||||
private NameValueEntry[] arNve = new NameValueEntry[1];
|
||||
|
||||
@@ -29,10 +31,11 @@ public class RedisService : IRedisService {
|
||||
public bool InitSubscriber() {
|
||||
try {
|
||||
_channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal(SUB_NAME));
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine(ex);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (_channel is null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -43,14 +46,11 @@ public class RedisService : IRedisService {
|
||||
public async Task<bool> InitStreamer() {
|
||||
try {
|
||||
_database = _cache.GetDatabase();
|
||||
if (!(await _database.KeyExistsAsync(STREAM_NAME)) ||
|
||||
(await _database.StreamGroupInfoAsync(STREAM_NAME)).All(x => x.Name != GROUP_NAME)) {
|
||||
await _database.StreamCreateConsumerGroupAsync(STREAM_NAME, GROUP_NAME, "0-0", true);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine(ex);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (_database is null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -59,13 +59,13 @@ public class RedisService : IRedisService {
|
||||
_database.StreamAddAsync(STREAM_NAME, arNve);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CanvasCommand>> Consume(string id) {
|
||||
public async Task<IEnumerable<CanvasCommand>> Consume() {
|
||||
List<CanvasCommand> lsComm = new();
|
||||
CanvasCommand? comm;
|
||||
var result = await _database.StreamReadGroupAsync(STREAM_NAME, GROUP_NAME, id, ">", 100);
|
||||
var result = await _database.StreamReadAsync(STREAM_NAME, _lastId, 100);
|
||||
string json = "";
|
||||
bool ok = false;
|
||||
foreach (var c in result) {
|
||||
_database.StreamAcknowledgeAsync(STREAM_NAME, GROUP_NAME, c.Id);
|
||||
try {
|
||||
json = c.Values.FirstOrDefault(x => x.Name == "command").Value.ToString();
|
||||
comm = JsonConvert.DeserializeObject<CanvasCommand>(json);
|
||||
@@ -74,7 +74,11 @@ public class RedisService : IRedisService {
|
||||
continue; }
|
||||
if (comm is not null)
|
||||
lsComm.Add(comm);
|
||||
if (!ok)
|
||||
ok = true;
|
||||
}
|
||||
if (ok)
|
||||
_lastId = result.LastOrDefault().Id.ToString()?? "0-0";
|
||||
return lsComm;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user