From ff99c383da8e8c697fdd0e9d235ea1cf6feb8abb Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 31 Jul 2014 23:51:34 -0700 Subject: [PATCH] XBMC version is cached by host & port Cache is cleared on test Version is cached for 12 hours --- .../Notifications/Xbmc/XbmcService.cs | 32 +++++++++++++------ .../Notifications/Xbmc/XbmcSettings.cs | 5 +++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs b/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs index 4d2e3d5da..a6f335eda 100644 --- a/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs +++ b/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs @@ -4,6 +4,7 @@ using FluentValidation.Results; using Newtonsoft.Json.Linq; using NLog; +using NzbDrone.Common.Cache; using NzbDrone.Common.Serializer; using NzbDrone.Core.Notifications.Xbmc.Model; using NzbDrone.Core.Tv; @@ -25,11 +26,18 @@ public class XbmcService : IXbmcService private readonly IEnumerable _apiProviders; private readonly Logger _logger; - public XbmcService(IXbmcJsonApiProxy proxy, IEnumerable apiProviders, Logger logger) + private readonly ICached _xbmcVersionCache; + + public XbmcService(IXbmcJsonApiProxy proxy, + IEnumerable apiProviders, + ICacheManager cacheManager, + Logger logger) { _proxy = proxy; _apiProviders = apiProviders; _logger = logger; + + _xbmcVersionCache = cacheManager.GetCache(GetType()); } public void Notify(XbmcSettings settings, string title, string message) @@ -54,20 +62,22 @@ public XbmcVersion GetJsonVersion(XbmcSettings settings) { try { - var response = _proxy.GetJsonVersion(settings); + return _xbmcVersionCache.Get(settings.Address, () => + { - _logger.Debug("Getting version from response: " + response); - var result = Json.Deserialize>(response); + var response = _proxy.GetJsonVersion(settings); - var versionObject = result.Result.Property("version"); + _logger.Debug("Getting version from response: " + response); + var result = Json.Deserialize>(response); - if (versionObject.Value.Type == JTokenType.Integer) - return new XbmcVersion((int)versionObject.Value); + var versionObject = result.Result.Property("version"); - if (versionObject.Value.Type == JTokenType.Object) - return Json.Deserialize(versionObject.Value.ToString()); + if (versionObject.Value.Type == JTokenType.Integer) return new XbmcVersion((int) versionObject.Value); - throw new InvalidCastException("Unknown Version structure!: " + versionObject); + if (versionObject.Value.Type == JTokenType.Object) return Json.Deserialize(versionObject.Value.ToString()); + + throw new InvalidCastException("Unknown Version structure!: " + versionObject); + }, TimeSpan.FromHours(12)); } catch (Exception ex) @@ -94,6 +104,8 @@ private IApiProvider GetApiProvider(XbmcSettings settings) public ValidationFailure Test(XbmcSettings settings) { + _xbmcVersionCache.Clear(); + try { _logger.Debug("Determining version of XBMC Host: {0}", settings.Address); diff --git a/src/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs b/src/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs index 055e02af8..edce3ac06 100644 --- a/src/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs +++ b/src/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs @@ -70,5 +70,10 @@ public ValidationResult Validate() { return Validator.Validate(this); } + + public override int GetHashCode() + { + return base.GetHashCode(); + } } }