The TimedQueueCache works similarly to the SimpleQueueCache but it also has a Timespan which is used as an expiration and retention time. I.e, only items that expire their Timespan (and were consumed by all cursors of course) are allowed to be removed from the cache. That way the cache always guarantees to hold all the items that were inserted in a certain Timespan (for example if the Timespan is 1 hour, all the messages that were inserted in the last hour will remain in the cache, with no regard if they were consumed or not). The TimedQueueCache also offers to hold a callback for when items are being removed from the cache and also allows to define an interval for how many items need to be removed before the callback is called.
Наследование: IQueueCache
        public virtual IBatchContainer GetCurrent(out Exception exception)
        {
            TimedQueueCache.Log(_logger, "TimedQueueCacheCursor.GetCurrent: {0}", _current);

            exception = null;
            return(_current);
        }
        public void InsertOneMessageEmptyCollection()
        {
            List<IBatchContainer> msgs = new List<IBatchContainer>();

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromHours(1), _defaultCacheSize, _defaultCacheBucketNum, _logger);
            cache.AddToCache(msgs);

            Assert.AreEqual(0, cache.Size);
        }
        public void InsertOneMessageTest()
        {
            Mock<IBatchContainer> batchMock = new Mock<IBatchContainer>();
            List<IBatchContainer> msgs = new List<IBatchContainer>(){batchMock.Object};

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromHours(1), _defaultCacheSize, _defaultCacheBucketNum, _logger);
            cache.AddToCache(msgs);

            Assert.AreEqual(1, cache.Size);
        }
 public TimedQueueCacheCursor(TimedQueueCache cache, Guid streamGuid, string streamNamespace, ILogger logger)
 {
     if (cache == null)
     {
         throw new ArgumentNullException(nameof(cache));
     }
     _cache           = cache;
     _streamGuid      = streamGuid;
     _streamNamespace = streamNamespace;
     _logger          = logger;
     _current         = null;
     TimedQueueCache.Log(logger, "TimedQueueCacheCursor New Cursor for {0}, {1}", streamGuid, streamNamespace);
 }
 public TimedQueueCacheCursor(TimedQueueCache cache, Guid streamGuid, string streamNamespace, Logger logger)
 {
     if (cache == null)
     {
         throw new ArgumentNullException(nameof(cache));
     }
     _cache = cache;
     _streamGuid = streamGuid;
     _streamNamespace = streamNamespace;
     _logger = logger;
     _current = null;
     TimedQueueCache.Log(logger, "TimedQueueCacheCursor New Cursor for {0}, {1}", streamGuid, streamNamespace);
 }
        public void MessageRemovalTest()
        {
            Mock<IBatchContainer> batchMock1 = new Mock<IBatchContainer>();
            Mock<IBatchContainer> batchMock2 = new Mock<IBatchContainer>();
            Mock<IBatchContainer> batchMock3 = new Mock<IBatchContainer>();

            List<IBatchContainer> msgs = new List<IBatchContainer>() { batchMock1.Object, batchMock2.Object, batchMock3.Object };

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromSeconds(1), _defaultCacheSize, _defaultCacheBucketNum, _logger);
            cache.AddToCache(msgs);

            Task.Delay(TimeSpan.FromSeconds(2)).Wait();

            IList<IBatchContainer> outContainers = new List<IBatchContainer>();

            cache.TryPurgeFromCache(out outContainers);

            Assert.AreEqual(0, cache.Size);
            Assert.AreEqual(3, outContainers.Count);
        }
        public void CursorRunningOnCacheMessagesFromMultipleStreamsGettingCursorWithOldTokenTest()
        {
            Guid streamGuid = Guid.NewGuid();
            string streamNamespace = "TestTimedCache";

            Guid otherGuid = Guid.NewGuid();
            string otherNamespace = "Other";

            Mock<IBatchContainer> batchMock1 = GenerateBatchContainerMock(streamGuid, streamNamespace, 1);
            Mock<IBatchContainer> batchMock2 = GenerateBatchContainerMock(otherGuid, streamNamespace, 2);
            Mock<IBatchContainer> batchMock3 = GenerateBatchContainerMock(streamGuid, otherNamespace, 3);
            Mock<IBatchContainer> batchMock4 = GenerateBatchContainerMock(otherGuid, otherNamespace, 4);
            Mock<IBatchContainer> batchMock5 = GenerateBatchContainerMock(streamGuid, streamNamespace, 5);
            Mock<IBatchContainer> batchMock6 = GenerateBatchContainerMock(streamGuid, streamNamespace, 6);

            List<IBatchContainer> msgs = new List<IBatchContainer>() { batchMock2.Object, batchMock3.Object, batchMock4.Object, batchMock5.Object, batchMock6.Object };

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromHours(1), _defaultCacheSize, _defaultCacheBucketNum, _logger);
            cache.AddToCache(msgs);

            Exception placeholder;

            var cursor = cache.GetCacheCursor(new StreamKey(streamGuid, streamNamespace), batchMock1.Object.SequenceToken);

            Assert.AreEqual(5, cache.Size);

            Assert.IsTrue(cursor.MoveNext(), "Couldn't move next on cursor");
            Assert.AreEqual(cursor.GetCurrent(out placeholder), batchMock5.Object, "Didn't get the first object");
            Assert.IsTrue(cursor.MoveNext(), "Couldn't move next on cursor");
            Assert.AreEqual(cursor.GetCurrent(out placeholder), batchMock6.Object, "Didn't get the second object");
        }
        public void MaxAddCountIsBucketSizeBecauseCacheIsFullTest()
        {
            Guid streamGuid = Guid.NewGuid();
            string streamNamespace = "TestTimedCache";

            Mock<IBatchContainer> batchMock1 = GenerateBatchContainerMock(streamGuid, streamNamespace, 1);
            Mock<IBatchContainer> batchMock2 = GenerateBatchContainerMock(streamGuid, streamNamespace, 2);
            Mock<IBatchContainer> batchMock3 = GenerateBatchContainerMock(streamGuid, streamNamespace, 3);
            Mock<IBatchContainer> batchMock4 = GenerateBatchContainerMock(streamGuid, streamNamespace, 4);
            Mock<IBatchContainer> batchMock5 = GenerateBatchContainerMock(streamGuid, streamNamespace, 5);
            Mock<IBatchContainer> batchMock6 = GenerateBatchContainerMock(streamGuid, streamNamespace, 6);

            List<IBatchContainer> msgs = new List<IBatchContainer>() { batchMock1.Object, batchMock2.Object, batchMock3.Object, batchMock4.Object, batchMock5.Object, batchMock6.Object };

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromSeconds(1), msgs.Count, msgs.Count / 2, _logger);
            cache.AddToCache(msgs);

            Task.Delay(TimeSpan.FromSeconds(2)).Wait();

            Assert.IsTrue(!cache.IsUnderPressure());
            Assert.AreEqual(2, cache.GetMaxAddCount());
        }
        public void CacheIsNotUnderPressureBecauseTimespanHasPassedAndNoSlowConsumersTest()
        {
            Guid streamGuid = Guid.NewGuid();
            string streamNamespace = "TestTimedCache";

            Mock<IBatchContainer> batchMock1 = GenerateBatchContainerMock(streamGuid, streamNamespace, 1);
            Mock<IBatchContainer> batchMock2 = GenerateBatchContainerMock(streamGuid, streamNamespace, 2);
            Mock<IBatchContainer> batchMock3 = GenerateBatchContainerMock(streamGuid, streamNamespace, 3);

            List<IBatchContainer> msgs = new List<IBatchContainer>() { batchMock1.Object, batchMock2.Object, batchMock3.Object };

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromSeconds(1), msgs.Count, _defaultCacheBucketNum, _logger);
            cache.AddToCache(msgs);

            Task.Delay(TimeSpan.FromSeconds(2)).Wait();

            Assert.IsTrue(!cache.IsUnderPressure());
            Assert.AreEqual(1, cache.GetMaxAddCount());
        }
        public void CacheUnderPressureBecauseOfSlowConsumersTest()
        {
            Guid streamGuid = Guid.NewGuid();
            string streamNamespace = "TestTimedCache";

            Mock<IBatchContainer> batchMock1 = GenerateBatchContainerMock(streamGuid, streamNamespace, 1);
            Mock<IBatchContainer> batchMock2 = GenerateBatchContainerMock(streamGuid, streamNamespace, 2);
            Mock<IBatchContainer> batchMock3 = GenerateBatchContainerMock(streamGuid, streamNamespace, 3);

            List<IBatchContainer> msgs = new List<IBatchContainer>() { batchMock1.Object, batchMock2.Object, batchMock3.Object };

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromSeconds(1), msgs.Count, _defaultCacheBucketNum, _logger);
            cache.AddToCache(msgs);

            var cursor = cache.GetCacheCursor(new StreamKey(streamGuid, streamNamespace), batchMock1.Object.SequenceToken);

            Task.Delay(TimeSpan.FromSeconds(2)).Wait();

            Assert.IsTrue(cache.IsUnderPressure());
        }
        public void CacheUnderPressureBecauseTimespanGuaranteeTest()
        {
            Guid streamGuid = Guid.NewGuid();
            string streamNamespace = "TestTimedCache";

            Mock<IBatchContainer> batchMock1 = GenerateBatchContainerMock(streamGuid, streamNamespace, 1);
            Mock<IBatchContainer> batchMock2 = GenerateBatchContainerMock(streamGuid, streamNamespace, 2);
            Mock<IBatchContainer> batchMock3 = GenerateBatchContainerMock(streamGuid, streamNamespace, 3);

            List<IBatchContainer> msgs = new List<IBatchContainer>() { batchMock1.Object, batchMock2.Object, batchMock3.Object };

            var cache = new TimedQueueCache(_defaultId, TimeSpan.FromHours(1), msgs.Count, _defaultCacheBucketNum, _logger);
            cache.AddToCache(msgs);

            Assert.IsTrue(cache.IsUnderPressure());
        }