/// <inheritdoc/> public ValueTask <TScope?> FindByNameAsync(string name, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException(SR.GetResourceString(SR.ID0202), nameof(name)); } var parameters = new { Method = nameof(FindByNameAsync), Name = name }; if (_cache.TryGetValue(parameters, out TScope? scope)) { return(new ValueTask <TScope?>(scope)); } async Task <TScope?> ExecuteAsync() { if ((scope = await _store.FindByNameAsync(name, cancellationToken)) is not null) { await AddAsync(scope, cancellationToken); } await CreateEntryAsync(parameters, scope, cancellationToken); return(scope); } return(new ValueTask <TScope?>(ExecuteAsync())); }
/// <summary> /// Retrieves a scope using its name. /// </summary> /// <param name="name">The name associated with the scope.</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 scope corresponding to the specified name. /// </returns> public ValueTask <TScope> FindByNameAsync([NotNull] string name, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("The scope name cannot be null or empty.", nameof(name)); } var parameters = new { Method = nameof(FindByNameAsync), Name = name }; if (_cache.TryGetValue(parameters, out TScope scope)) { return(new ValueTask <TScope>(scope)); } async Task <TScope> ExecuteAsync() { if ((scope = await _store.FindByNameAsync(name, cancellationToken)) != null) { await AddAsync(scope, cancellationToken); } await CreateEntryAsync(parameters, scope, cancellationToken); return(scope); } return(new ValueTask <TScope>(ExecuteAsync())); }
/// <summary> /// Retrieves a scope using its name. /// </summary> /// <param name="name">The name associated with the scope.</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 scope corresponding to the specified name. /// </returns> public ValueTask <TScope> FindByNameAsync([NotNull] string name, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("The scope name cannot be null or empty.", nameof(name)); } var parameters = new { Method = nameof(FindByNameAsync), Name = name }; if (_cache.TryGetValue(parameters, out TScope scope)) { return(new ValueTask <TScope>(scope)); } async Task <TScope> ExecuteAsync() { if ((scope = await _store.FindByNameAsync(name, cancellationToken)) != null) { await AddAsync(scope, cancellationToken); } using (var entry = _cache.CreateEntry(parameters)) { if (scope != null) { var signal = await CreateExpirationSignalAsync(scope, cancellationToken); if (signal == null) { throw new InvalidOperationException("An error occurred while creating an expiration signal."); } entry.AddExpirationToken(signal); } entry.SetSize(1L); entry.SetValue(scope); } return(scope); } return(new ValueTask <TScope>(ExecuteAsync())); }