Revert "提交"

This reverts commit b96d895809.
This commit is contained in:
2026-02-12 21:59:08 +08:00
parent b96d895809
commit d429560511
339 changed files with 46657 additions and 46655 deletions
+46 -46
View File
@@ -1,46 +1,46 @@
using IM_API.Interface.Services;
using IM_API.Models;
using IM_API.Tools;
using Microsoft.EntityFrameworkCore;
using RedLockNet;
using StackExchange.Redis;
namespace IM_API.Services
{
public class SequenceIdService : ISequenceIdService
{
private IDatabase _database;
private IDistributedLockFactory _lockFactory;
private ImContext _context;
public SequenceIdService(IConnectionMultiplexer connectionMultiplexer,
IDistributedLockFactory distributedLockFactory, ImContext imContext)
{
_database = connectionMultiplexer.GetDatabase();
_lockFactory = distributedLockFactory;
_context = imContext;
}
public async Task<long> GetNextSquenceIdAsync(string streamKey)
{
string key = RedisKeys.GetSequenceIdKey(streamKey);
string lockKey = RedisKeys.GetSequenceIdLockKey(streamKey);
var exists = await _database.KeyExistsAsync(key);
if (!exists)
{
using (var _lock = await _lockFactory.CreateLockAsync(lockKey, TimeSpan.FromSeconds(5)))
{
if (_lock.IsAcquired)
{
if(!await _database.KeyExistsAsync(key))
{
var max = await _context.Messages
.Where(x => x.StreamKey == streamKey)
.MaxAsync(m => (long?)m.SequenceId) ?? 0;
await _database.StringSetAsync(key, max, TimeSpan.FromDays(7));
}
}
}
}
return await _database.StringIncrementAsync(key);
}
}
}
using IM_API.Interface.Services;
using IM_API.Models;
using IM_API.Tools;
using Microsoft.EntityFrameworkCore;
using RedLockNet;
using StackExchange.Redis;
namespace IM_API.Services
{
public class SequenceIdService : ISequenceIdService
{
private IDatabase _database;
private IDistributedLockFactory _lockFactory;
private ImContext _context;
public SequenceIdService(IConnectionMultiplexer connectionMultiplexer,
IDistributedLockFactory distributedLockFactory, ImContext imContext)
{
_database = connectionMultiplexer.GetDatabase();
_lockFactory = distributedLockFactory;
_context = imContext;
}
public async Task<long> GetNextSquenceIdAsync(string streamKey)
{
string key = RedisKeys.GetSequenceIdKey(streamKey);
string lockKey = RedisKeys.GetSequenceIdLockKey(streamKey);
var exists = await _database.KeyExistsAsync(key);
if (!exists)
{
using (var _lock = await _lockFactory.CreateLockAsync(lockKey, TimeSpan.FromSeconds(5)))
{
if (_lock.IsAcquired)
{
if(!await _database.KeyExistsAsync(key))
{
var max = await _context.Messages
.Where(x => x.StreamKey == streamKey)
.MaxAsync(m => (long?)m.SequenceId) ?? 0;
await _database.StringSetAsync(key, max, TimeSpan.FromDays(7));
}
}
}
}
return await _database.StringIncrementAsync(key);
}
}
}