public int GetMany(ReadOnlySpan <TKey> keys, Span <KeyValuePair <TKey, TValue> > destination)
        {
            var stopwatch = StopwatchStruct.StartNew();

            try
            {
                var countFound = _innerCache.GetMany(keys, destination);

                var values = destination.Slice(0, countFound);

                OnGetManyCompletedSuccessfully(keys, values, stopwatch.Elapsed);

                return(countFound);
            }
            catch (Exception ex)
            {
                OnGetManyException(keys, stopwatch.Elapsed, ex, out var exceptionHandled);

                if (!exceptionHandled)
                {
                    throw;
                }

                return(0);
            }
        }
        public bool TryRemove(TKey key, out TValue value)
        {
            var stopwatch = StopwatchStruct.StartNew();

            try
            {
                var removed = _innerCache.TryRemove(key, out value);

                OnTryRemoveCompletedSuccessfully(key, removed, value, stopwatch.Elapsed);

                return(removed);
            }
            catch (Exception ex)
            {
                OnTryRemoveException(key, stopwatch.Elapsed, ex, out var exceptionHandled);

                if (!exceptionHandled)
                {
                    throw;
                }

                value = default;
                return(false);
            }
        }
Esempio n. 3
0
        private protected async Task UpdateValueWithinLock(
            Func <T, TUpdates, CancellationToken, Task <T> > applyUpdatesFunc,
            TUpdates updates,
            CancellationToken cancellationToken)
        {
            var stopwatch = StopwatchStruct.StartNew();

            try
            {
                var previousValue = _value;
                _value = await applyUpdatesFunc(_value, updates, cancellationToken).ConfigureAwait(false);

                Interlocked.Increment(ref _version);

                PublishValueUpdatedEvent(previousValue, updates, stopwatch.Elapsed);
            }
            catch (Exception ex)
            {
                PublishValueUpdateExceptionEvent(ex, updates, stopwatch.Elapsed);
                throw;
            }
            finally
            {
                ReleaseRefreshOrUpdateValueLock();
            }
        }
        public void MeasuresTimeAccurately()
        {
            var timer1 = StopwatchStruct.StartNew();
            var timer2 = Stopwatch.StartNew();

            Thread.Sleep(TimeSpan.FromSeconds(1));

            timer1.Elapsed.Should().BeCloseTo(timer2.Elapsed);
        }
        public void Set(TKey key, TValue value, TimeSpan timeToLive)
        {
            var stopwatch = StopwatchStruct.StartNew();

            try
            {
                _innerCache.Set(key, value, timeToLive);

                OnSetCompletedSuccessfully(key, value, timeToLive, stopwatch.Elapsed);
            }
            catch (Exception ex)
            {
                OnSetException(key, value, timeToLive, stopwatch.Elapsed, ex, out var exceptionHandled);

                if (!exceptionHandled)
                {
                    throw;
                }
            }
        }
        public void SetMany(ReadOnlySpan <KeyValuePair <TKey, TValue> > values, TimeSpan timeToLive)
        {
            var stopwatch = StopwatchStruct.StartNew();

            try
            {
                _innerCache.SetMany(values, timeToLive);

                OnSetManyCompletedSuccessfully(values, timeToLive, stopwatch.Elapsed);
            }
            catch (Exception ex)
            {
                OnSetManyException(values, timeToLive, stopwatch.Elapsed, ex, out var exceptionHandled);

                if (!exceptionHandled)
                {
                    throw;
                }
            }
        }
        public async Task <(bool Success, ValueAndTimeToLive <TValue> Value)> TryGet(TKey key)
        {
            var stopwatch = StopwatchStruct.StartNew();

            try
            {
                var result = await _innerCache
                             .TryGet(key)
                             .ConfigureAwait(false);

                OnTryGetCompletedSuccessfully(key, result.Success, result.Value, stopwatch.Elapsed);

                return(result);
            }
            catch (Exception ex)
            {
                OnTryGetException(key, stopwatch.Elapsed, ex, out var exceptionHandled);

                if (!exceptionHandled)
                {
                    throw;
                }

                return(default);
Esempio n. 8
0
        public async Task InitializeAsync(CancellationToken cancellationToken = default)
        {
            TaskCompletionSource <bool> tcs;
            var initializationAlreadyInProgress = false;

            lock (_lock)
            {
                switch (_state)
                {
                case Disposed:
                    throw GetObjectDisposedException();

                case Ready:
                    return;

                case InitializationInProgress:
                    tcs = _initializationTaskCompletionSource;
                    initializationAlreadyInProgress = true;
                    break;

                case PendingInitialization:
                    tcs    = _initializationTaskCompletionSource = new TaskCompletionSource <bool>();
                    _state = InitializationInProgress;
                    break;

                default:
                    throw new InvalidOperationException("Invalid state - " + _state);
                }
            }

            if (initializationAlreadyInProgress)
            {
                await tcs.Task.ConfigureAwait(false);

                return;
            }

            var start     = DateTime.UtcNow;
            var stopwatch = StopwatchStruct.StartNew();

            CancellationTokenSource cts = null;

            try
            {
                if (_refreshValueFuncTimeout.HasValue)
                {
                    cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

                    cts.CancelAfter(_refreshValueFuncTimeout.Value);

                    cancellationToken = cts.Token;
                }

                _value = await _getValueFunc(cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                lock (_lock)
                {
                    ThrowIfDisposed();
                    _state = PendingInitialization;
                    _initializationTaskCompletionSource = null;
                }

                PublishValueRefreshExceptionEvent(ex, stopwatch.Elapsed);
                tcs.TrySetException(ex);

                throw;
            }
            finally
            {
                cts?.Dispose();
            }

            lock (_lock)
            {
                ThrowIfDisposed();
                _version = 1;
                _state   = Ready;
                _initializationTaskCompletionSource = null;
            }

            OnInitialized?.Invoke(this, null);
            tcs.TrySetResult(true);

            PublishValueRefreshedEvent(default, stopwatch.Elapsed);