update network cache

Conflicts:
	MediaBrowser.Common.Implementations/Networking/BaseNetworkManager.cs
pull/702/head
Luke Pulverenti 9 years ago
parent 74d85e8b19
commit 83d96f9bda

@ -6,7 +6,6 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading;
using MoreLinq; using MoreLinq;
namespace MediaBrowser.Common.Implementations.Networking namespace MediaBrowser.Common.Implementations.Networking
@ -14,22 +13,11 @@ namespace MediaBrowser.Common.Implementations.Networking
public abstract class BaseNetworkManager public abstract class BaseNetworkManager
{ {
protected ILogger Logger { get; private set; } protected ILogger Logger { get; private set; }
private Timer _clearCacheTimer; private DateTime _lastRefresh;
protected BaseNetworkManager(ILogger logger) protected BaseNetworkManager(ILogger logger)
{ {
Logger = logger; Logger = logger;
// Can't use network change events due to a crash in Linux
_clearCacheTimer = new Timer(ClearCacheTimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
}
private void ClearCacheTimerCallback(object state)
{
lock (_localIpAddressSyncLock)
{
_localIpAddresses = null;
}
} }
private volatile List<IPAddress> _localIpAddresses; private volatile List<IPAddress> _localIpAddresses;
@ -41,15 +29,21 @@ namespace MediaBrowser.Common.Implementations.Networking
/// <returns>IPAddress.</returns> /// <returns>IPAddress.</returns>
public IEnumerable<IPAddress> GetLocalIpAddresses() public IEnumerable<IPAddress> GetLocalIpAddresses()
{ {
if (_localIpAddresses == null) const int cacheMinutes = 3;
var forceRefresh = (DateTime.UtcNow - _lastRefresh).TotalMinutes >= cacheMinutes;
if (_localIpAddresses == null || forceRefresh)
{ {
lock (_localIpAddressSyncLock) lock (_localIpAddressSyncLock)
{ {
if (_localIpAddresses == null) forceRefresh = (DateTime.UtcNow - _lastRefresh).TotalMinutes >= cacheMinutes;
if (_localIpAddresses == null || forceRefresh)
{ {
var addresses = GetLocalIpAddressesInternal().ToList(); var addresses = GetLocalIpAddressesInternal().ToList();
_localIpAddresses = addresses; _localIpAddresses = addresses;
_lastRefresh = DateTime.UtcNow;
return addresses; return addresses;
} }

Loading…
Cancel
Save