コード例 #1
0
        public async Task TestQueriesDiskCache()
        {
            await _bufferedDiskCache.Put(_cacheKey, _encodedImage);

            EncodedImage image = await _bufferedDiskCache.Get(_cacheKey, _isCancelled);

            Assert.IsTrue(2 == image.GetByteBufferRef().GetUnderlyingReferenceTestOnly().GetRefCountTestOnly());
            byte[] buf1 = new byte[_pooledByteBuffer.Size];
            _pooledByteBuffer.Read(0, buf1, 0, _pooledByteBuffer.Size);
            byte[] buf2 = new byte[image.GetByteBufferRef().Get().Size];
            image.GetByteBufferRef().Get().Read(0, buf2, 0, image.GetByteBufferRef().Get().Size);
            CollectionAssert.AreEqual(buf1, buf2);
        }
コード例 #2
0
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(
            IConsumer <EncodedImage> consumer,
            IProducerContext producerContext)
        {
            ImageRequest imageRequest = producerContext.ImageRequest;

            if (!imageRequest.IsDiskCacheEnabled)
            {
                MaybeStartInputProducer(consumer, consumer, producerContext);
                return;
            }

            producerContext.Listener.OnProducerStart(producerContext.Id, PRODUCER_NAME);
            ICacheKey cacheKey = _cacheKeyFactory.GetEncodedCacheKey(
                imageRequest, producerContext.CallerContext);

            bool isSmallRequest = (imageRequest.CacheChoice == CacheChoice.SMALL);
            BufferedDiskCache preferredCache = isSmallRequest ?
                                               _smallImageBufferedDiskCache :
                                               _defaultBufferedDiskCache;

            AtomicBoolean       isCancelled = new AtomicBoolean(false);
            Task <EncodedImage> diskLookupTask;

            if (_chooseCacheByImageSize)
            {
                bool alreadyInSmall = _smallImageBufferedDiskCache.ContainsSync(cacheKey);
                bool alreadyInMain  = _defaultBufferedDiskCache.ContainsSync(cacheKey);
                BufferedDiskCache firstCache;
                BufferedDiskCache secondCache;
                if (alreadyInSmall || !alreadyInMain)
                {
                    firstCache  = _smallImageBufferedDiskCache;
                    secondCache = _defaultBufferedDiskCache;
                }
                else
                {
                    firstCache  = _defaultBufferedDiskCache;
                    secondCache = _smallImageBufferedDiskCache;
                }

                diskLookupTask = firstCache.Get(cacheKey, isCancelled);
                diskLookupTask = diskLookupTask.ContinueWith(
                    task =>
                {
                    if (IsTaskCancelled(task) || (!task.IsFaulted && task.Result != null))
                    {
                        return(task);
                    }

                    return(secondCache.Get(cacheKey, isCancelled));
                },
                    TaskContinuationOptions.ExecuteSynchronously)
                                 .Unwrap();
            }
            else
            {
                diskLookupTask = preferredCache.Get(cacheKey, isCancelled);
            }

            diskLookupTask.ContinueWith(
                task =>
            {
                OnFinishDiskReads(
                    task,
                    consumer,
                    preferredCache,
                    cacheKey,
                    producerContext);
            },
                TaskContinuationOptions.ExecuteSynchronously);

            SubscribeTaskForRequestCancellation(isCancelled, producerContext);
        }