1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

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 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<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,6 +62,9 @@ public XbmcVersion GetJsonVersion(XbmcSettings settings)
{
try
{
return _xbmcVersionCache.Get(settings.Address, () =>
{
var response = _proxy.GetJsonVersion(settings);
_logger.Debug("Getting version from response: " + response);
@ -61,13 +72,12 @@ public XbmcVersion GetJsonVersion(XbmcSettings settings)
var versionObject = result.Result.Property("version");
if (versionObject.Value.Type == JTokenType.Integer)
return new XbmcVersion((int)versionObject.Value);
if (versionObject.Value.Type == JTokenType.Integer) return new XbmcVersion((int) versionObject.Value);
if (versionObject.Value.Type == JTokenType.Object)
return Json.Deserialize<XbmcVersion>(versionObject.Value.ToString());
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 @@ private IApiProvider GetApiProvider(XbmcSettings settings)
public ValidationFailure Test(XbmcSettings settings)
{
_xbmcVersionCache.Clear();
try
{
_logger.Debug("Determining version of XBMC Host: {0}", settings.Address);

View File

@ -70,5 +70,10 @@ public ValidationResult Validate()
{
return Validator.Validate(this);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}