diff --git a/src/Contracts/Masa.Mc.Contracts.Admin/Consts/CacheKeys.cs b/src/Contracts/Masa.Mc.Contracts.Admin/Consts/CacheKeys.cs index bc4c88c8..e3179d5a 100644 --- a/src/Contracts/Masa.Mc.Contracts.Admin/Consts/CacheKeys.cs +++ b/src/Contracts/Masa.Mc.Contracts.Admin/Consts/CacheKeys.cs @@ -6,4 +6,5 @@ namespace Masa.Mc.Contracts.Admin.Consts; public class CacheKeys { public const string MESSAGE_CURSOR_CHECK_COUNT = nameof(MESSAGE_CURSOR_CHECK_COUNT); + public const string GET_NOTICE_LIST = nameof(GET_NOTICE_LIST); } \ No newline at end of file diff --git a/src/Services/Masa.Mc.Service/Application/WebsiteMessages/EventHandler/RemoveWebsiteMessageCacheEventHandler.cs b/src/Services/Masa.Mc.Service/Application/WebsiteMessages/EventHandler/RemoveWebsiteMessageCacheEventHandler.cs new file mode 100644 index 00000000..307985a8 --- /dev/null +++ b/src/Services/Masa.Mc.Service/Application/WebsiteMessages/EventHandler/RemoveWebsiteMessageCacheEventHandler.cs @@ -0,0 +1,19 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the Apache License. See LICENSE.txt in the project root for license information. + +namespace Masa.Mc.Service.Admin.Application.WebsiteMessages.EventHandler; + +public class RemoveWebsiteMessageCacheEventHandler +{ + private readonly IDistributedCacheClient _cacheClient; + public RemoveWebsiteMessageCacheEventHandler(IDistributedCacheClient cacheClient) + { + _cacheClient = cacheClient; + } + [EventHandler] + public async Task HandleEvent(RemoveWebsiteMessageCacheEvent eto) + { + var cacheKey = CacheKeys.GET_NOTICE_LIST + "-" + eto.UserId; + await _cacheClient.RemoveAsync>(cacheKey); + } +} diff --git a/src/Services/Masa.Mc.Service/Application/WebsiteMessages/WebsiteMessageQueryHandler.cs b/src/Services/Masa.Mc.Service/Application/WebsiteMessages/WebsiteMessageQueryHandler.cs index c030cc7e..1fd0674f 100644 --- a/src/Services/Masa.Mc.Service/Application/WebsiteMessages/WebsiteMessageQueryHandler.cs +++ b/src/Services/Masa.Mc.Service/Application/WebsiteMessages/WebsiteMessageQueryHandler.cs @@ -8,14 +8,17 @@ public class WebsiteMessageQueryHandler private readonly IMcQueryContext _context; private readonly IUserContext _userContext; private readonly II18n _i18n; + private IDistributedCacheClient _cache; public WebsiteMessageQueryHandler(IMcQueryContext context , IUserContext userContext - , II18n i18n) + , II18n i18n + , IDistributedCacheClient cache) { _context = context; _userContext = userContext; _i18n = i18n; + _cache = cache; } [EventHandler] @@ -77,6 +80,15 @@ public async Task GetChannelListAsync(GetChannelListWebsiteMessageQuery query) public async Task GetNoticeListAsync(GetNoticeListQuery query) { var userId = _userContext.GetUserId(); + var cacheKey = CacheKeys.GET_NOTICE_LIST + "-" + userId; + + var response = _cache.Get>(cacheKey); + if (response != null) + { + query.Result = response; + return; + } + var noticeNum = query.PageSize; var queryable = _context.WebsiteMessageQueries.Include(x => x.Channel).Where(x => x.Channel != null && x.UserId == userId && !x.IsWithdrawn); var list = await queryable.Where(x => !x.IsRead).OrderByDescending(x => x.CreationTime).Take(noticeNum).ToListAsync(); @@ -88,7 +100,13 @@ public async Task GetNoticeListAsync(GetNoticeListQuery query) } var dtos = list.Adapt>(); FillListDtos(dtos); - query.Result = dtos; + + if (dtos.Any()) + { + _cache.Set(cacheKey, dtos ?? new(), DateTimeOffset.Now.AddMinutes(10)); + } + + query.Result = dtos ?? new(); } private async Task>> CreateFilteredPredicate(GetWebsiteMessageInputDto inputDto) diff --git a/src/Services/Masa.Mc.Service/Domain/WebsiteMessages/Aggregates/WebsiteMessage.cs b/src/Services/Masa.Mc.Service/Domain/WebsiteMessages/Aggregates/WebsiteMessage.cs index 6cdf839e..9919caed 100644 --- a/src/Services/Masa.Mc.Service/Domain/WebsiteMessages/Aggregates/WebsiteMessage.cs +++ b/src/Services/Masa.Mc.Service/Domain/WebsiteMessages/Aggregates/WebsiteMessage.cs @@ -50,6 +50,11 @@ public WebsiteMessage(Guid messageTaskHistoryId, Guid channelId, Guid userId, st ExtraProperties = extraProperties; AddTag(); + + if (userId != Guid.Empty) + { + AddDomainEvent(new RemoveWebsiteMessageCacheEvent(userId)); + } } public void SetRead() diff --git a/src/Services/Masa.Mc.Service/Domain/WebsiteMessages/Events/RemoveWebsiteMessageCacheEvent.cs b/src/Services/Masa.Mc.Service/Domain/WebsiteMessages/Events/RemoveWebsiteMessageCacheEvent.cs new file mode 100644 index 00000000..fc9ae0a8 --- /dev/null +++ b/src/Services/Masa.Mc.Service/Domain/WebsiteMessages/Events/RemoveWebsiteMessageCacheEvent.cs @@ -0,0 +1,8 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the Apache License. See LICENSE.txt in the project root for license information. + +namespace Masa.Mc.Service.Admin.Domain.WebsiteMessages.Events; + +public record RemoveWebsiteMessageCacheEvent(Guid UserId) : DomainEvent +{ +}