The Free Software Media System - Server Backend & API
Clean up TunerHost classes
Patrick Barron5c65abcd948b
diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.csindex a8d34d19c8..6b8b82bc20 100644--- a/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs+++ b/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs@@ -1,10 +1,10 @@#pragma warning disable CS1591using System;-using System.Collections.Concurrent;using System.Collections.Generic;using System.IO;using System.Linq;+using System.Text.Json;using System.Threading;using System.Threading.Tasks;using MediaBrowser.Common.Configuration;@@ -14,7 +14,7 @@ using MediaBrowser.Controller.LiveTv;using MediaBrowser.Model.Dto;using MediaBrowser.Model.IO;using MediaBrowser.Model.LiveTv;-using MediaBrowser.Model.Serialization;+using Microsoft.Extensions.Caching.Memory;using Microsoft.Extensions.Logging;namespace Emby.Server.Implementations.LiveTv.TunerHosts@@ -23,17 +23,15 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts{protected readonly IServerConfigurationManager Config;protected readonly ILogger<BaseTunerHost> Logger;- protected IJsonSerializer JsonSerializer;protected readonly IFileSystem FileSystem;- private readonly ConcurrentDictionary<string, ChannelCache> _channelCache =- new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase);+ private readonly IMemoryCache _memoryCache;- protected BaseTunerHost(IServerConfigurationManager config, ILogger<BaseTunerHost> logger, IJsonSerializer jsonSerializer, IFileSystem fileSystem)+ protected BaseTunerHost(IServerConfigurationManager config, ILogger<BaseTunerHost> logger, IFileSystem fileSystem, IMemoryCache memoryCache){Config = config;Logger = logger;- JsonSerializer = jsonSerializer;+ _memoryCache = memoryCache;FileSystem = fileSystem;}@@ -44,23 +42,19 @@ namespace Emby.Server.Implementations.LiveTv.TunerHostspublic async Task<List<ChannelInfo>> GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken){- ChannelCache cache = null;var key = tuner.Id;- if (enableCache && !string.IsNullOrEmpty(key) && _channelCache.TryGetValue(key, out cache))+ if (enableCache && !string.IsNullOrEmpty(key) && _memoryCache.TryGetValue(key, out List<ChannelInfo> cache)){- return cache.Channels.ToList();+ return cache;}- var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);- var list = result.ToList();+ var list = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);// logger.LogInformation("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list));if (!string.IsNullOrEmpty(key) && list.Count > 0){- cache = cache ?? new ChannelCache();- cache.Channels = list;- _channelCache.AddOrUpdate(key, cache, (k, v) => cache);+ _memoryCache.CreateEntry(key).SetValue(list);}return list;@@ -95,7 +89,8 @@ namespace Emby.Server.Implementations.LiveTv.TunerHoststry{Directory.CreateDirectory(Path.GetDirectoryName(channelCacheFile));- JsonSerializer.SerializeToFile(channels, channelCacheFile);+ await using var writeStream = File.OpenWrite(channelCacheFile);+ await JsonSerializer.SerializeAsync(writeStream, channels, cancellationToken: cancellationToken).ConfigureAwait(false);}catch (IOException){@@ -110,7 +105,9 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts{try{- var channels = JsonSerializer.DeserializeFromFile<List<ChannelInfo>>(channelCacheFile);+ await using var readStream = File.OpenRead(channelCacheFile);+ var channels = await JsonSerializer.DeserializeAsync<List<ChannelInfo>>(readStream, cancellationToken: cancellationToken)+ .ConfigureAwait(false);list.AddRange(channels);}catch (IOException)@@ -233,10 +230,5 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts{return Config.GetConfiguration<LiveTvOptions>("livetv");}-- private class ChannelCache- {- public List<ChannelInfo> Channels;- }}}diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.csindex 00420bd2ad..c61189c0ac 100644--- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs+++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs@@ -7,6 +7,7 @@ using System.IO;using System.Linq;using System.Net;using System.Net.Http;+using System.Text.Json;using System.Threading;using System.Threading.Tasks;using MediaBrowser.Common.Configuration;@@ -23,7 +24,7 @@ using MediaBrowser.Model.IO;using MediaBrowser.Model.LiveTv;using MediaBrowser.Model.MediaInfo;using MediaBrowser.Model.Net;-using MediaBrowser.Model.Serialization;+using Microsoft.Extensions.Caching.Memory;using Microsoft.Extensions.Logging;namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun@@ -39,14 +40,14 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerunpublic HdHomerunHost(IServerConfigurationManager config,ILogger<HdHomerunHost> logger,- IJsonSerializer jsonSerializer,IFileSystem fileSystem,IHttpClient httpClient,IServerApplicationHost appHost,ISocketFactory socketFactory,INetworkManager networkManager,- IStreamHelper streamHelper)- : base(config, logger, jsonSerializer, fileSystem)+ IStreamHelper streamHelper,+ IMemoryCache memoryCache)+ : base(config, logger, fileSystem, memoryCache){_httpClient = httpClient;_appHost = appHost;@@ -75,18 +76,17 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerunBufferContent = false};- using (var response = await _httpClient.SendAsync(options, HttpMethod.Get).ConfigureAwait(false))- using (var stream = response.Content)- {- var lineup = await JsonSerializer.DeserializeFromStreamAsync<List<Channels>>(stream).ConfigureAwait(false) ?? new List<Channels>();-- if (info.ImportFavoritesOnly)- {- lineup = lineup.Where(i => i.Favorite).ToList();- }+ using var response = await _httpClient.SendAsync(options, HttpMethod.Get).ConfigureAwait(false);+ await using var stream = response.Content;+ var lineup = await JsonSerializer.DeserializeAsync<List<Channels>>(stream, cancellationToken: cancellationToken)+ .ConfigureAwait(false) ?? new List<Channels>();- return lineup.Where(i => !i.DRM).ToList();+ if (info.ImportFavoritesOnly)+ {+ lineup = lineup.Where(i => i.Favorite).ToList();}++ return lineup.Where(i => !i.DRM).ToList();}private class HdHomerunChannelInfo : ChannelInfo@@ -132,30 +132,30 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomeruntry{- using (var response = await _httpClient.SendAsync(new HttpRequestOptions()+ using var response = await _httpClient.SendAsync(+ new HttpRequestOptions{- Url = string.Format("{0}/discover.json", GetApiUrl(info)),+ Url = string.Format(CultureInfo.InvariantCulture, "{0}/discover.json", GetApiUrl(info)),CancellationToken = cancellationToken,BufferContent = false- }, HttpMethod.Get).ConfigureAwait(false))- using (var stream = response.Content)- {- var discoverResponse = await JsonSerializer.DeserializeFromStreamAsync<DiscoverResponse>(stream).ConfigureAwait(false);+ }, HttpMethod.Get).ConfigureAwait(false);+ await using var stream = response.Content;+ var discoverResponse = await JsonSerializer.DeserializeAsync<DiscoverResponse>(stream, cancellationToken: cancellationToken)+ .ConfigureAwait(false);- if (!string.IsNullOrEmpty(cacheKey))+ if (!string.IsNullOrEmpty(cacheKey))+ {+ lock (_modelCache){- lock (_modelCache)- {- _modelCache[cacheKey] = discoverResponse;- }+ _modelCache[cacheKey] = discoverResponse;}-- return discoverResponse;}++ return discoverResponse;}catch (HttpException ex){- if (!throwAllExceptions && ex.StatusCode.HasValue && ex.StatusCode.Value == System.Net.HttpStatusCode.NotFound)+ if (!throwAllExceptions && ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound){var defaultValue = "HDHR";var response = new DiscoverResponsediff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.csindex ff42a9747f..8fc29fb4a2 100644--- a/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs+++ b/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs@@ -18,7 +18,7 @@ using MediaBrowser.Model.Entities;using MediaBrowser.Model.IO;using MediaBrowser.Model.LiveTv;using MediaBrowser.Model.MediaInfo;-using MediaBrowser.Model.Serialization;+using Microsoft.Extensions.Caching.Memory;using Microsoft.Extensions.Logging;using Microsoft.Net.Http.Headers;@@ -36,13 +36,13 @@ namespace Emby.Server.Implementations.LiveTv.TunerHostsIServerConfigurationManager config,IMediaSourceManager mediaSourceManager,ILogger<M3UTunerHost> logger,- IJsonSerializer jsonSerializer,IFileSystem fileSystem,IHttpClient httpClient,IServerApplicationHost appHost,INetworkManager networkManager,- IStreamHelper streamHelper)- : base(config, logger, jsonSerializer, fileSystem)+ IStreamHelper streamHelper,+ IMemoryCache memoryCache)+ : base(config, logger, fileSystem, memoryCache){_httpClient = httpClient;_appHost = appHost;