Exemple #1
0
        /// <summary>
        /// Add the specified scope to the cache.
        /// </summary>
        /// <param name="scope">The scope to add to the cache.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
        /// <returns>
        /// A <see cref="Task"/> that can be used to monitor the asynchronous operation.
        /// </returns>
        public async Task AddAsync([NotNull] TScope scope, CancellationToken cancellationToken)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            _cache.Remove(new
            {
                Method     = nameof(FindByIdAsync),
                Identifier = await _store.GetIdAsync(scope, cancellationToken)
            });

            _cache.Remove(new
            {
                Method = nameof(FindByNameAsync),
                Name   = await _store.GetNameAsync(scope, cancellationToken)
            });

            foreach (var resource in await _store.GetResourcesAsync(scope, cancellationToken))
            {
                _cache.Remove(new
                {
                    Method   = nameof(FindByResourceAsync),
                    Resource = resource
                });
            }

            var signal = await CreateExpirationSignalAsync(scope, cancellationToken);

            if (signal == null)
            {
                throw new InvalidOperationException("An error occurred while creating an expiration token.");
            }

            using (var entry = _cache.CreateEntry(new
            {
                Method = nameof(FindByIdAsync),
                Identifier = await _store.GetIdAsync(scope, cancellationToken)
            }))
            {
                entry.AddExpirationToken(signal)
                .SetSize(1L)
                .SetValue(scope);
            }

            using (var entry = _cache.CreateEntry(new
            {
                Method = nameof(FindByNameAsync),
                Name = await _store.GetNameAsync(scope, cancellationToken)
            }))
            {
                entry.AddExpirationToken(signal)
                .SetSize(1L)
                .SetValue(scope);
            }
        }
Exemple #2
0
        /// <inheritdoc/>
        public async ValueTask AddAsync(TScope scope, CancellationToken cancellationToken)
        {
            if (scope is null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            _cache.Remove(new
            {
                Method     = nameof(FindByIdAsync),
                Identifier = await _store.GetIdAsync(scope, cancellationToken)
            });

            _cache.Remove(new
            {
                Method = nameof(FindByNameAsync),
                Name   = await _store.GetNameAsync(scope, cancellationToken)
            });

            foreach (var resource in await _store.GetResourcesAsync(scope, cancellationToken))
            {
                _cache.Remove(new
                {
                    Method   = nameof(FindByResourceAsync),
                    Resource = resource
                });
            }

            await CreateEntryAsync(new
            {
                Method     = nameof(FindByIdAsync),
                Identifier = await _store.GetIdAsync(scope, cancellationToken)
            }, scope, cancellationToken);

            await CreateEntryAsync(new
            {
                Method = nameof(FindByNameAsync),
                Name   = await _store.GetNameAsync(scope, cancellationToken)
            }, scope, cancellationToken);
        }