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

View File

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

View File

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