Asyncify NotificationManager

This commit is contained in:
nicolas.dorier
2020-06-17 11:26:21 +09:00
parent 9f12fe7e0a
commit 0b720768b8
2 changed files with 7 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ using BTCPayServer.Data;
using BTCPayServer.Models.NotificationViewModels; using BTCPayServer.Models.NotificationViewModels;
using Google.Apis.Storage.v1.Data; using Google.Apis.Storage.v1.Data;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -31,20 +32,20 @@ namespace BTCPayServer.Services.Notifications
} }
private const int _cacheExpiryMs = 5000; private const int _cacheExpiryMs = 5000;
public NotificationSummaryViewModel GetSummaryNotifications(ClaimsPrincipal user) public async Task<NotificationSummaryViewModel> GetSummaryNotifications(ClaimsPrincipal user)
{ {
var userId = _userManager.GetUserId(user); var userId = _userManager.GetUserId(user);
if (_memoryCache.TryGetValue<NotificationSummaryViewModel>(userId, out var obj)) if (_memoryCache.TryGetValue<NotificationSummaryViewModel>(userId, out var obj))
return obj; return obj;
var resp = FetchNotificationsFromDb(userId); var resp = await FetchNotificationsFromDb(userId);
_memoryCache.Set(userId, resp, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs))); _memoryCache.Set(userId, resp, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs)));
return resp; return resp;
} }
private NotificationSummaryViewModel FetchNotificationsFromDb(string userId) private async Task<NotificationSummaryViewModel> FetchNotificationsFromDb(string userId)
{ {
var resp = new NotificationSummaryViewModel(); var resp = new NotificationSummaryViewModel();
using (var _db = _factory.CreateContext()) using (var _db = _factory.CreateContext())
@@ -57,10 +58,11 @@ namespace BTCPayServer.Services.Notifications
{ {
try try
{ {
resp.Last5 = _db.Notifications resp.Last5 = (await _db.Notifications
.Where(a => a.ApplicationUserId == userId && !a.Seen) .Where(a => a.ApplicationUserId == userId && !a.Seen)
.OrderByDescending(a => a.Created) .OrderByDescending(a => a.Created)
.Take(5) .Take(5)
.ToListAsync())
.Select(a => ToViewModel(a)) .Select(a => ToViewModel(a))
.ToList(); .ToList();
} }

View File

@@ -1,7 +1,7 @@
@inject BTCPayServer.Services.Notifications.NotificationManager notificationManager @inject BTCPayServer.Services.Notifications.NotificationManager notificationManager
@{ @{
var notificationModel = notificationManager.GetSummaryNotifications(User); var notificationModel = await notificationManager.GetSummaryNotifications(User);
} }
@if (notificationModel.UnseenCount > 0) @if (notificationModel.UnseenCount > 0)