Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reactor: GetNoticeListAsync use cache #551

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Contracts/Masa.Mc.Contracts.Admin/Consts/CacheKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<List<WebsiteMessageDto>>(cacheKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ public class WebsiteMessageQueryHandler
private readonly IMcQueryContext _context;
private readonly IUserContext _userContext;
private readonly II18n<DefaultResource> _i18n;
private IDistributedCacheClient _cache;

public WebsiteMessageQueryHandler(IMcQueryContext context
, IUserContext userContext
, II18n<DefaultResource> i18n)
, II18n<DefaultResource> i18n
, IDistributedCacheClient cache)
{
_context = context;
_userContext = userContext;
_i18n = i18n;
_cache = cache;
}

[EventHandler]
Expand Down Expand Up @@ -77,6 +80,15 @@ public async Task GetChannelListAsync(GetChannelListWebsiteMessageQuery query)
public async Task GetNoticeListAsync(GetNoticeListQuery query)
{
var userId = _userContext.GetUserId<Guid>();
var cacheKey = CacheKeys.GET_NOTICE_LIST + "-" + userId;

var response = _cache.Get<List<WebsiteMessageDto>>(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();
Expand All @@ -88,7 +100,13 @@ public async Task GetNoticeListAsync(GetNoticeListQuery query)
}
var dtos = list.Adapt<List<WebsiteMessageDto>>();
FillListDtos(dtos);
query.Result = dtos;

if (dtos.Any())
{
_cache.Set(cacheKey, dtos ?? new(), DateTimeOffset.Now.AddMinutes(10));
}

query.Result = dtos ?? new();
}

private async Task<Expression<Func<WebsiteMessageQueryModel, bool>>> CreateFilteredPredicate(GetWebsiteMessageInputDto inputDto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
}