Ejemplo n.º 1
0
        public Task <T> Get()
        {
            if (CurrentState == CacheState.Active ||
                CurrentState == CacheState.Reloading)
            {
                var result = Task.FromResult(Volatile.Read(ref _value));
                if (_stopWatchThreadSafe.Elapsed() > _expiresAfterFq)
                {
                    this.ChangeState(CacheEvent.Expire);
                }
                return(result);
            }
            else
            {
                var task = new TaskCompletionSource <T>();
                lock (_lock)
                {
                    _pendingTasks.Add(task);
                }

                // Need to do this check since we can't do an atomic add.
                if (CurrentState == CacheState.Active)
                {
                    task.TrySetResult(_value);
                }
                this.ChangeState(CacheEvent.Load);
                return(task.Task);
            }
        }
        public void SimpleTest()
        {
            var watch = new StopWatchThreadSafe();

            Thread.Sleep(5);
            Assert.AreEqual(StopWatchThreadSafe.MillsToFrequency(5), watch.Elapsed(), 500000);
        }
 private bool EligibleForRemoval(long timeIdle)
 {
     return(LastUsed.Elapsed() > timeIdle);
 }