update locking

This commit is contained in:
Luke Pulverenti 2016-02-24 00:36:58 -05:00
parent 94fa4dce4d
commit f5df8253ae
3 changed files with 23 additions and 39 deletions

View File

@ -20,7 +20,7 @@ namespace MediaBrowser.Common.Implementations.Networking
Logger = logger;
}
private volatile List<IPAddress> _localIpAddresses;
private List<IPAddress> _localIpAddresses;
private readonly object _localIpAddressSyncLock = new object();
/// <summary>
@ -29,24 +29,20 @@ namespace MediaBrowser.Common.Implementations.Networking
/// <returns>IPAddress.</returns>
public IEnumerable<IPAddress> GetLocalIpAddresses()
{
const int cacheMinutes = 3;
var forceRefresh = (DateTime.UtcNow - _lastRefresh).TotalMinutes >= cacheMinutes;
const int cacheMinutes = 5;
if (_localIpAddresses == null || forceRefresh)
lock (_localIpAddressSyncLock)
{
lock (_localIpAddressSyncLock)
var forceRefresh = (DateTime.UtcNow - _lastRefresh).TotalMinutes >= cacheMinutes;
if (_localIpAddresses == null || forceRefresh)
{
forceRefresh = (DateTime.UtcNow - _lastRefresh).TotalMinutes >= cacheMinutes;
var addresses = GetLocalIpAddressesInternal().ToList();
if (_localIpAddresses == null || forceRefresh)
{
var addresses = GetLocalIpAddressesInternal().ToList();
_localIpAddresses = addresses;
_lastRefresh = DateTime.UtcNow;
_localIpAddresses = addresses;
_lastRefresh = DateTime.UtcNow;
return addresses;
}
return addresses;
}
}

View File

@ -103,7 +103,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
Logger = logger;
_fileSystem = fileSystem;
ReloadTriggerEvents(true);
InitTriggerEvents();
}
/// <summary>
@ -233,11 +233,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
/// <summary>
/// The _triggers
/// </summary>
private volatile List<ITaskTrigger> _triggers;
/// <summary>
/// The _triggers sync lock
/// </summary>
private readonly object _triggersSyncLock = new object();
private List<ITaskTrigger> _triggers;
/// <summary>
/// Gets the triggers that define when the task will run
/// </summary>
@ -247,17 +243,6 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
{
get
{
if (_triggers == null)
{
lock (_triggersSyncLock)
{
if (_triggers == null)
{
_triggers = LoadTriggers();
}
}
}
return _triggers;
}
set
@ -303,6 +288,12 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
}
}
private void InitTriggerEvents()
{
_triggers = LoadTriggers();
ReloadTriggerEvents(true);
}
public void ReloadTriggerEvents()
{
ReloadTriggerEvents(false);

View File

@ -13,7 +13,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
where T : class
{
private readonly object _fileDataLock = new object();
private volatile List<T> _items;
private List<T> _items;
private readonly IJsonSerializer _jsonSerializer;
protected readonly ILogger Logger;
private readonly string _dataPath;
@ -31,17 +31,14 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
public IReadOnlyList<T> GetAll()
{
if (_items == null)
lock (_fileDataLock)
{
lock (_fileDataLock)
if (_items == null)
{
if (_items == null)
{
_items = GetItemsFromFile(_dataPath);
}
_items = GetItemsFromFile(_dataPath);
}
return _items;
}
return _items;
}
private List<T> GetItemsFromFile(string path)