public async Task <int> GetUserUnreadCountAsync() { var activeUserId = GetActiveUserId(); var siteId = GetCurrentSiteId(); var cacheKey = UnreadMailCacheKey(siteId, activeUserId); int?cachedUnreadCount = null; try { cachedUnreadCount = await _cache.GetIntFromCacheAsync(cacheKey); } catch (GraException gex) { _logger.LogWarning(gex, "Problem looking up unread count for {UserId}: {ErrorMessage}", activeUserId, gex.Message); } if (!cachedUnreadCount.HasValue) { await SendUserBroadcastsAsync(activeUserId, true); int unreadCount = await _mailRepository.GetUserUnreadCountAsync(activeUserId); await _cache.SaveToCacheAsync(cacheKey, unreadCount, ExpireInTimeSpan()); return(unreadCount); } else { return(cachedUnreadCount.Value); } }
public async Task <int> GetUserUnreadCountAsync() { var activeUserId = GetActiveUserId(); var cacheKey = $"{CacheKey.UserUnreadMailCount}?u{activeUserId}"; int unreadCount; if (!_memoryCache.TryGetValue(cacheKey, out unreadCount)) { unreadCount = await _mailRepository.GetUserUnreadCountAsync(activeUserId); _memoryCache.Set(cacheKey, unreadCount, new MemoryCacheEntryOptions() .SetAbsoluteExpiration(TimeSpan.FromMinutes(5))); } return(unreadCount); }
public async Task <int> GetUserUnreadCountAsync() { var activeUserId = GetActiveUserId(); var siteId = GetCurrentSiteId(); var cacheKey = UnreadMailCacheKey(siteId, activeUserId); var cachedUnreadCount = _cache.GetString(cacheKey); if (string.IsNullOrEmpty(cachedUnreadCount)) { await SendUserBroadcastsAsync(activeUserId, true); int unreadCount = await _mailRepository.GetUserUnreadCountAsync(activeUserId); _cache.SetString(cacheKey, unreadCount.ToString(), ExpireIn()); return(unreadCount); } else { return(int.Parse(cachedUnreadCount)); } }