public ChannelFeatures() { MediaTypes = new ChannelMediaType[] { }; ContentTypes = new ChannelMediaContentType[] { }; DefaultSortFields = new ChannelItemSortField[] { }; }
private async Task<ChannelItemResult> GetChannelItems(IChannel channel, User user, string folderId, int? startIndex, int? limit, ChannelItemSortField? sortField, bool sortDescending, CancellationToken cancellationToken) { var userId = user.Id.ToString("N"); var cacheLength = CacheLength; var cachePath = GetChannelDataCachePath(channel, userId, folderId, sortField, sortDescending); try { if (!startIndex.HasValue && !limit.HasValue) { if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow) { return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath); } } } catch (FileNotFoundException) { } catch (DirectoryNotFoundException) { } await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); try { try { if (!startIndex.HasValue && !limit.HasValue) { if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow) { return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath); } } } catch (FileNotFoundException) { } catch (DirectoryNotFoundException) { } var query = new InternalChannelItemQuery { UserId = userId, StartIndex = startIndex, Limit = limit, SortBy = sortField, SortDescending = sortDescending }; if (!string.IsNullOrWhiteSpace(folderId)) { var categoryItem = (IChannelItem)_libraryManager.GetItemById(new Guid(folderId)); query.FolderId = categoryItem.ExternalId; } var result = await channel.GetChannelItems(query, cancellationToken).ConfigureAwait(false); if (!startIndex.HasValue && !limit.HasValue) { CacheResponse(result, cachePath); } return result; } finally { _resourcePool.Release(); } }
private string GetChannelDataCachePath(IChannel channel, string userId, string folderId, ChannelItemSortField? sortField, bool sortDescending) { var channelId = GetInternalChannelId(channel.Name).ToString("N"); var userCacheKey = string.Empty; var hasCacheKey = channel as IHasCacheKey; if (hasCacheKey != null) { userCacheKey = hasCacheKey.GetCacheKey(userId) ?? string.Empty; } var filename = string.IsNullOrWhiteSpace(folderId) ? "root" : folderId; filename += userCacheKey; var version = (channel.DataVersion ?? string.Empty).GetMD5().ToString("N"); if (sortField.HasValue) { filename += "-sortField-" + sortField.Value; } if (sortDescending) { filename += "-sortDescending"; } filename = filename.GetMD5().ToString("N"); return Path.Combine(_config.ApplicationPaths.CachePath, "channels", channelId, version, filename + ".json"); }