public async Task <AttemptsDB_AttemptDTO> GetActiveAttemptByMemberId(ObjectId memberId) { AttemptsDB_AttemptDTO attempt; if (!_internalActiveByMemberIdCache.ContainsKey(memberId) || _internalActiveByMemberIdCache[memberId] != null && !_internalByIdCache.ContainsKey(_internalActiveByMemberIdCache[memberId].Value)) { attempt = await _redisCache.Get($"MemberActiveAttempt:{memberId}", async() => await AttemptWorker.GetActiveAttemptByMemberId(memberId)); if (attempt != null) { _internalActiveByMemberIdCache.TryAdd(memberId, attempt.Id); _internalByIdCache.TryAdd(attempt.Id, attempt); } else { _internalActiveByMemberIdCache.TryAdd(memberId, null); } } else { attempt = _internalActiveByMemberIdCache[memberId] != null ? _internalByIdCache[_internalActiveByMemberIdCache[memberId].Value] : null; } return(attempt); }
public async Task <AttemptsDB_AttemptDTO[]> GetAttemptsByTestId(ObjectId testId) { AttemptsDB_AttemptDTO[] attempts = new AttemptsDB_AttemptDTO[0]; var cached = _internalByTestIdCache.ContainsKey(testId); if (cached) { attempts = _internalByTestIdCache[testId] .Where(attemptId => _internalByIdCache.ContainsKey(attemptId) ).Select(attemptId => _internalByIdCache[attemptId] ).ToArray(); } if (!cached || attempts.Length != _internalByTestIdCache[testId].Length) { attempts = await AttemptWorker.GetAttemptsByTestId(testId); var attemptIds = new ObjectId[attempts.Length]; for (var i = 0; i < attempts.Length; i++) { _internalByIdCache.TryAdd(attempts[i].Id, attempts[i]); attemptIds[i] = attempts[i].Id; } _internalByTestIdCache.TryAdd(testId, attemptIds); } return(attempts); }
public async Task <AttemptsDB_AttemptDTO> GetAttemptById(ObjectId attemptId) { AttemptsDB_AttemptDTO attempt; if (_internalByIdCache.ContainsKey(attemptId)) { attempt = _internalByIdCache[attemptId]; } else { attempt = await AttemptWorker.GetAttemptById(attemptId); _internalByIdCache.Add(attemptId, attempt); } return(attempt); }