XBMC version is cached by host & port

Cache is cleared on test
Version is cached for 12 hours
pull/4/head
Mark McDowall 10 years ago
parent fc46018c9a
commit ff99c383da

@ -4,6 +4,7 @@ using System.Linq;
using FluentValidation.Results; using FluentValidation.Results;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NLog; using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.Notifications.Xbmc.Model; using NzbDrone.Core.Notifications.Xbmc.Model;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -25,11 +26,18 @@ namespace NzbDrone.Core.Notifications.Xbmc
private readonly IEnumerable<IApiProvider> _apiProviders; private readonly IEnumerable<IApiProvider> _apiProviders;
private readonly Logger _logger; private readonly Logger _logger;
public XbmcService(IXbmcJsonApiProxy proxy, IEnumerable<IApiProvider> apiProviders, Logger logger) private readonly ICached<XbmcVersion> _xbmcVersionCache;
public XbmcService(IXbmcJsonApiProxy proxy,
IEnumerable<IApiProvider> apiProviders,
ICacheManager cacheManager,
Logger logger)
{ {
_proxy = proxy; _proxy = proxy;
_apiProviders = apiProviders; _apiProviders = apiProviders;
_logger = logger; _logger = logger;
_xbmcVersionCache = cacheManager.GetCache<XbmcVersion>(GetType());
} }
public void Notify(XbmcSettings settings, string title, string message) public void Notify(XbmcSettings settings, string title, string message)
@ -54,20 +62,22 @@ namespace NzbDrone.Core.Notifications.Xbmc
{ {
try try
{ {
var response = _proxy.GetJsonVersion(settings); return _xbmcVersionCache.Get(settings.Address, () =>
{
_logger.Debug("Getting version from response: " + response); var response = _proxy.GetJsonVersion(settings);
var result = Json.Deserialize<XbmcJsonResult<JObject>>(response);
var versionObject = result.Result.Property("version"); _logger.Debug("Getting version from response: " + response);
var result = Json.Deserialize<XbmcJsonResult<JObject>>(response);
if (versionObject.Value.Type == JTokenType.Integer) var versionObject = result.Result.Property("version");
return new XbmcVersion((int)versionObject.Value);
if (versionObject.Value.Type == JTokenType.Object) if (versionObject.Value.Type == JTokenType.Integer) return new XbmcVersion((int) versionObject.Value);
return Json.Deserialize<XbmcVersion>(versionObject.Value.ToString());
throw new InvalidCastException("Unknown Version structure!: " + versionObject); if (versionObject.Value.Type == JTokenType.Object) return Json.Deserialize<XbmcVersion>(versionObject.Value.ToString());
throw new InvalidCastException("Unknown Version structure!: " + versionObject);
}, TimeSpan.FromHours(12));
} }
catch (Exception ex) catch (Exception ex)
@ -94,6 +104,8 @@ namespace NzbDrone.Core.Notifications.Xbmc
public ValidationFailure Test(XbmcSettings settings) public ValidationFailure Test(XbmcSettings settings)
{ {
_xbmcVersionCache.Clear();
try try
{ {
_logger.Debug("Determining version of XBMC Host: {0}", settings.Address); _logger.Debug("Determining version of XBMC Host: {0}", settings.Address);

@ -70,5 +70,10 @@ namespace NzbDrone.Core.Notifications.Xbmc
{ {
return Validator.Validate(this); return Validator.Validate(this);
} }
public override int GetHashCode()
{
return base.GetHashCode();
}
} }
} }

Loading…
Cancel
Save