Optimize caching on chat member

🐛 Trying to fix uploading file permission check
This commit is contained in:
2025-05-25 20:18:27 +08:00
parent cbe913e535
commit 33767a6d7f
6 changed files with 37 additions and 46 deletions

View File

@ -9,6 +9,7 @@ public class ChatRoomService(AppDatabase db, ICacheService cache)
{
public const string ChatRoomGroupPrefix = "ChatRoom_";
private const string RoomMembersCacheKeyPrefix = "ChatRoomMembers_";
private const string ChatMemberCacheKey = "ChatMember_{0}_{1}";
public async Task<List<ChatMember>> ListRoomMembers(Guid roomId)
{
@ -31,6 +32,25 @@ public class ChatRoomService(AppDatabase db, ICacheService cache)
return members;
}
public async Task<ChatMember?> GetChannelMember(Guid accountId, Guid chatRoomId)
{
var cacheKey = string.Format(ChatMemberCacheKey, accountId, chatRoomId);
var member = await cache.GetAsync<ChatMember?>(cacheKey);
if (member is not null) return member;
member = await db.ChatMembers
.Where(m => m.AccountId == accountId && m.ChatRoomId == chatRoomId)
.FirstOrDefaultAsync();
if (member == null) return member;
var chatRoomGroup = ChatRoomGroupPrefix + chatRoomId;
await cache.SetWithGroupsAsync(cacheKey, member,
[chatRoomGroup],
TimeSpan.FromMinutes(5));
return member;
}
public async Task PurgeRoomMembersCache(Guid roomId)
{
var chatRoomGroup = ChatRoomGroupPrefix + roomId;