Example #1
0
        public void TestTimeoutCacheGetOrAddExisting()
        {
            //Arrange
            AsyncStaticTimeoutCache <int, object> cache = new AsyncStaticTimeoutCache <int, object>(TimeSpan.FromMinutes(1));
            object existingValue = new object();
            object newValue      = new object();

            cache.GetValue(1, _ => Task.FromResult(existingValue));

            //Act
            object cacheValue = cache.GetValue(1, _ => Task.FromResult(newValue)).Result;

            //Assert
            Assert.IsTrue(ReferenceEquals(existingValue, cacheValue));
        }
Example #2
0
        public async Task <bool> TrySaveFanArt(Guid mediaItemId, string title, string fanArtType, TrySaveFanArtAsyncDelegate saveDlgt)
        {
            string fanArtCacheDirectory   = GetFanArtDirectory(mediaItemId);
            string fanArtTypeSubDirectory = GetFanArtTypeDirectory(fanArtCacheDirectory, fanArtType);

            using (var writer = await _fanArtSync.WriterLockAsync(mediaItemId).ConfigureAwait(false))
            {
                if (!await InitCache(fanArtCacheDirectory, fanArtTypeSubDirectory, title).ConfigureAwait(false))
                {
                    return(false);
                }
                FanArtCount currentCount = await _fanArtCounts.GetValue(CreateFanArtTypeId(mediaItemId, fanArtType), _ => CreateFanArtCount(mediaItemId, fanArtType)).ConfigureAwait(false);

                if (currentCount.Count < GetMaxFanArtCount(fanArtType) && await saveDlgt(fanArtTypeSubDirectory).ConfigureAwait(false))
                {
                    currentCount.Count++;
                    return(true);
                }
            }
            return(false);
        }
Example #3
0
        /// <summary>
        /// Tries to read a series nfo-file into <see cref="SeriesStub"/> objects (or gets them from cache)
        /// </summary>
        /// <param name="nfoFsra"><see cref="IFileSystemResourceAccessor"/> pointing to the nfo-file</param>
        /// <returns><c>true</c> if any usable metadata was found; else <c>false</c></returns>
        public override async Task <bool> TryReadMetadataAsync(IFileSystemResourceAccessor nfoFsra)
        {
            var stubs = await CACHE.GetValue(nfoFsra.CanonicalLocalResourcePath, async path =>
            {
                _debugLogger.Info("[#{0}]: SeriesStub object for series nfo-file not found in cache; parsing nfo-file {1}", _miNumber, nfoFsra.CanonicalLocalResourcePath);
                if (await base.TryReadMetadataAsync(nfoFsra).ConfigureAwait(false))
                {
                    if (_settings.EnableDebugLogging && _settings.WriteStubObjectIntoDebugLog)
                    {
                        LogStubObjects();
                    }
                    return(_stubs);
                }
                return(null);
            }).ConfigureAwait(false);

            if (stubs == null)
            {
                return(false);
            }
            _stubs = stubs;
            return(true);
        }