1、优化消息排序逻辑 2、新增加载历史消息 3、修复已知问题 后端: 1、优化消息排序逻辑 2、增加用户信息缓存机制 3、修改日期类型为DateTimeOffset改善时区信息丢失问题 3、修复了已知问题 数据库: 1、新增SequenceId字段用于消息排序 2、新增ClientMsgId字段用于客户端消息回执
47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|