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 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 @@ public class XbmcService : IXbmcService
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 @@ public XbmcVersion GetJsonVersion(XbmcSettings settings)
{ {
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 @@ private IApiProvider GetApiProvider(XbmcSettings settings)
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);

View File

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