XBMC version is cached by host & port

Cache is cleared on test
Version is cached for 12 hours
This commit is contained in:
Mark McDowall 2014-07-31 23:51:34 -07:00
parent fc46018c9a
commit ff99c383da
2 changed files with 27 additions and 10 deletions

View File

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

View File

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