/// <inheritdoc/> public ValueTask <TApplication?> FindByIdAsync(string identifier, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(identifier)) { throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); } var parameters = new { Method = nameof(FindByIdAsync), Identifier = identifier }; if (_cache.TryGetValue(parameters, out TApplication? application)) { return(new ValueTask <TApplication?>(application)); } return(new ValueTask <TApplication?>(ExecuteAsync())); async Task <TApplication?> ExecuteAsync() { if ((application = await _store.FindByIdAsync(identifier, cancellationToken)) is not null) { await AddAsync(application, cancellationToken); } await CreateEntryAsync(parameters, application, cancellationToken); return(application); } }
/// <summary> /// Retrieves an application using its unique identifier. /// </summary> /// <param name="identifier">The unique identifier associated with the application.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation, /// whose result returns the client application corresponding to the identifier. /// </returns> public ValueTask <TApplication> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(identifier)) { throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier)); } var parameters = new { Method = nameof(FindByIdAsync), Identifier = identifier }; if (_cache.TryGetValue(parameters, out TApplication application)) { return(new ValueTask <TApplication>(application)); } return(new ValueTask <TApplication>(ExecuteAsync())); async Task <TApplication> ExecuteAsync() { if ((application = await _store.FindByIdAsync(identifier, cancellationToken)) != null) { await AddAsync(application, cancellationToken); } await CreateEntryAsync(parameters, application, cancellationToken); return(application); } }
/// <summary> /// Retrieves an application using its unique identifier. /// </summary> /// <param name="identifier">The unique identifier associated with the application.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation, /// whose result returns the client application corresponding to the identifier. /// </returns> public ValueTask <TApplication> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(identifier)) { throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier)); } var parameters = new { Method = nameof(FindByIdAsync), Identifier = identifier }; if (_cache.TryGetValue(parameters, out TApplication application)) { return(new ValueTask <TApplication>(application)); } async Task <TApplication> ExecuteAsync() { if ((application = await _store.FindByIdAsync(identifier, cancellationToken)) != null) { await AddAsync(application, cancellationToken); } using (var entry = _cache.CreateEntry(parameters)) { if (application != null) { var signal = await CreateExpirationSignalAsync(application, cancellationToken); if (signal == null) { throw new InvalidOperationException("An error occurred while creating an expiration signal."); } entry.AddExpirationToken(signal); } entry.SetSize(1L); entry.SetValue(application); } return(application); } return(new ValueTask <TApplication>(ExecuteAsync())); }
public async Task FindByIdAsync_Should_Return_Null_If_Not_Found() { var nonExistingId = Guid.NewGuid().ToString(); var application = await _applicationStore.FindByIdAsync(nonExistingId, CancellationToken.None); application.ShouldBeNull(); }