protected static FanArtCount InitFanArtCount(string mediaItemId, string fanArtType) { lock (_fanArtCountSync) { _clearCountTimer.Change(FANART_CLEAN_DELAY, Timeout.Infinite); Dictionary <string, FanArtCount> mediaItemCounts; if (!_fanArtCounts.TryGetValue(mediaItemId, out mediaItemCounts)) { _fanArtCounts[mediaItemId] = mediaItemCounts = new Dictionary <string, FanArtCount>(); } FanArtCount count; if (!mediaItemCounts.TryGetValue(fanArtType, out count)) { mediaItemCounts[fanArtType] = count = new FanArtCount(GetFanArtFiles(mediaItemId, fanArtType).Count); } return(count); } }
public async Task <int> TrySaveFanArt <T>(Guid mediaItemId, string title, string fanArtType, ICollection <T> files, TrySaveMultipleFanArtAsyncDelegate <T> saveDlgt) { if (files == null || files.Count == 0) { return(0); } string fanArtCacheDirectory = GetFanArtDirectory(mediaItemId); string fanArtTypeSubDirectory = GetFanArtTypeDirectory(fanArtCacheDirectory, fanArtType); int savedCount = 0; using (var writer = await _fanArtSync.WriterLockAsync(mediaItemId).ConfigureAwait(false)) { if (!await InitCache(fanArtCacheDirectory, fanArtTypeSubDirectory, title).ConfigureAwait(false)) { return(savedCount); } int maxCount = GetMaxFanArtCount(fanArtType); FanArtCount currentCount = await _fanArtCounts.GetValue(CreateFanArtTypeId(mediaItemId, fanArtType), _ => CreateFanArtCount(mediaItemId, fanArtType)).ConfigureAwait(false); if (currentCount.Count >= maxCount) { return(savedCount); } foreach (T file in files) { if (await saveDlgt(fanArtTypeSubDirectory, file).ConfigureAwait(false)) { savedCount++; currentCount.Count++; if (currentCount.Count >= maxCount) { break; } } } } return(savedCount); }
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); }
public FanArtCountLock(FanArtCount count) { _count = count; Monitor.Enter(_count.SyncObj); Count = _count.Count; }