From 422219ab21bfaa627ef6ec779cdf4bbfe4911344 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 9 Oct 2017 16:01:22 +1100 Subject: [PATCH] Updates the cache file paths to be resolved behind a lazy so that there's proper locking in place for accessing the value, this also makes the code a little nicer. --- src/Umbraco.Core/PluginManager.cs | 78 +++++++------------ .../Sync/DatabaseServerMessenger.cs | 28 ++----- 2 files changed, 34 insertions(+), 72 deletions(-) diff --git a/src/Umbraco.Core/PluginManager.cs b/src/Umbraco.Core/PluginManager.cs index e31e5752c1..aafbc92b8b 100644 --- a/src/Umbraco.Core/PluginManager.cs +++ b/src/Umbraco.Core/PluginManager.cs @@ -41,8 +41,8 @@ namespace Umbraco.Core private readonly IServiceProvider _serviceProvider; private readonly IRuntimeCacheProvider _runtimeCache; private readonly ProfilingLogger _logger; - private string _pluginListFilePath; - private string _pluginHashFilePath; + private readonly Lazy _pluginListFilePath = new Lazy(GetPluginListFilePath); + private readonly Lazy _pluginHashFilePath = new Lazy(GetPluginHashFilePath); private readonly object _typesLock = new object(); private readonly Dictionary _types = new Dictionary(); @@ -69,8 +69,6 @@ namespace Umbraco.Core _runtimeCache = runtimeCache; _logger = logger; - var pluginListFile = GetPluginListFilePath(); - if (detectChanges) { //first check if the cached hash is string.Empty, if it is then we need @@ -82,8 +80,8 @@ namespace Umbraco.Core // if the hash has changed, clear out the persisted list no matter what, this will force // rescanning of all plugin types including lazy ones. // http://issues.umbraco.org/issue/U4-4789 - if(File.Exists(pluginListFile)) - File.Delete(pluginListFile); + if(File.Exists(_pluginListFilePath.Value)) + File.Delete(_pluginListFilePath.Value); WriteCachePluginsHash(); } @@ -93,8 +91,8 @@ namespace Umbraco.Core // if the hash has changed, clear out the persisted list no matter what, this will force // rescanning of all plugin types including lazy ones. // http://issues.umbraco.org/issue/U4-4789 - if (File.Exists(pluginListFile)) - File.Delete(pluginListFile); + if (File.Exists(_pluginListFilePath.Value)) + File.Delete(_pluginListFilePath.Value); // always set to true if we're not detecting (generally only for testing) RequiresRescanning = true; @@ -187,11 +185,10 @@ namespace Umbraco.Core { if (_cachedAssembliesHash != null) return _cachedAssembliesHash; + + if (File.Exists(_pluginHashFilePath.Value) == false) return string.Empty; - var filePath = GetPluginHashFilePath(); - if (File.Exists(filePath) == false) return string.Empty; - - var hash = File.ReadAllText(filePath, Encoding.UTF8); + var hash = File.ReadAllText(_pluginHashFilePath.Value, Encoding.UTF8); _cachedAssembliesHash = hash; return _cachedAssembliesHash; @@ -229,9 +226,8 @@ namespace Umbraco.Core /// Writes the assembly hash file. /// private void WriteCachePluginsHash() - { - var filePath = GetPluginHashFilePath(); - File.WriteAllText(filePath, CurrentAssembliesHash, Encoding.UTF8); + { + File.WriteAllText(_pluginHashFilePath.Value, CurrentAssembliesHash, Encoding.UTF8); } /// @@ -351,8 +347,7 @@ namespace Umbraco.Core { try { - var filePath = GetPluginListFilePath(); - File.Delete(filePath); + File.Delete(_pluginListFilePath.Value); } catch { @@ -366,12 +361,11 @@ namespace Umbraco.Core internal Dictionary, IEnumerable> ReadCache() { var cache = new Dictionary, IEnumerable>(); - - var filePath = GetPluginListFilePath(); - if (File.Exists(filePath) == false) + + if (File.Exists(_pluginListFilePath.Value) == false) return cache; - using (var stream = GetFileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, ListFileOpenReadTimeout)) + using (var stream = GetFileStream(_pluginListFilePath.Value, FileMode.Open, FileAccess.Read, FileShare.Read, ListFileOpenReadTimeout)) using (var reader = new StreamReader(stream)) { while (true) @@ -414,24 +408,17 @@ namespace Umbraco.Core /// Generally only used for resetting cache, for example during the install process. public void ClearPluginCache() { - var path = GetPluginListFilePath(); - if (File.Exists(path)) - File.Delete(path); - - path = GetPluginHashFilePath(); - if (File.Exists(path)) - File.Delete(path); + if (File.Exists(_pluginListFilePath.Value)) + File.Delete(_pluginListFilePath.Value); + + if (File.Exists(_pluginHashFilePath.Value)) + File.Delete(_pluginHashFilePath.Value); _runtimeCache.ClearCacheItem(CacheKey); } - private string GetPluginListFilePath() - { - //if it's already set then return it - we don't care about locking here - //if 2 threads do this at the same time it won't hurt - if (_pluginListFilePath != null) - return _pluginListFilePath; - + private static string GetPluginListFilePath() + { string pluginListFilePath; switch (GlobalSettings.LocalTempStorageLocation) { @@ -461,17 +448,11 @@ namespace Umbraco.Core if (Directory.Exists(folder) == false) Directory.CreateDirectory(folder); - _pluginListFilePath = pluginListFilePath; - return _pluginListFilePath; + return pluginListFilePath; } - private string GetPluginHashFilePath() + private static string GetPluginHashFilePath() { - //if it's already set then return it - we don't care about locking here - //if 2 threads do this at the same time it won't hurt - if (_pluginHashFilePath != null) - return _pluginHashFilePath; - string pluginHashFilePath; switch (GlobalSettings.LocalTempStorageLocation) { @@ -500,16 +481,13 @@ namespace Umbraco.Core throw new InvalidOperationException("The folder could not be determined for the file " + pluginHashFilePath); if (Directory.Exists(folder) == false) Directory.CreateDirectory(folder); - - _pluginHashFilePath = pluginHashFilePath; - return _pluginHashFilePath; + + return pluginHashFilePath; } internal void WriteCache() { - var filePath = GetPluginListFilePath(); - - using (var stream = GetFileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, ListFileOpenWriteTimeout)) + using (var stream = GetFileStream(_pluginListFilePath.Value, FileMode.Create, FileAccess.Write, FileShare.None, ListFileOpenWriteTimeout)) using (var writer = new StreamWriter(stream)) { foreach (var typeList in _types.Values) @@ -755,7 +733,7 @@ namespace Umbraco.Core // else proceed, typeList = new TypeList(baseType, attributeType); - var scan = RequiresRescanning || File.Exists(GetPluginListFilePath()) == false; + var scan = RequiresRescanning || File.Exists(_pluginListFilePath.Value) == false; if (scan) { diff --git a/src/Umbraco.Core/Sync/DatabaseServerMessenger.cs b/src/Umbraco.Core/Sync/DatabaseServerMessenger.cs index 98c57e67df..8d0fb54b1e 100644 --- a/src/Umbraco.Core/Sync/DatabaseServerMessenger.cs +++ b/src/Umbraco.Core/Sync/DatabaseServerMessenger.cs @@ -40,7 +40,7 @@ namespace Umbraco.Core.Sync private bool _syncing; private bool _released; private readonly ProfilingLogger _profilingLogger; - private string _distCacheFilePath; + private Lazy _distCacheFilePath = new Lazy(GetDistCacheFilePath); protected DatabaseServerMessengerOptions Options { get; private set; } protected ApplicationContext ApplicationContext { get { return _appContext; } } @@ -462,10 +462,9 @@ namespace Umbraco.Core.Sync /// private void ReadLastSynced() { - var path = SyncFilePath; - if (File.Exists(path) == false) return; + if (File.Exists(_distCacheFilePath.Value) == false) return; - var content = File.ReadAllText(path); + var content = File.ReadAllText(_distCacheFilePath.Value); int last; if (int.TryParse(content, out last)) _lastId = last; @@ -480,7 +479,7 @@ namespace Umbraco.Core.Sync /// private void SaveLastSynced(int id) { - File.WriteAllText(SyncFilePath, id.ToString(CultureInfo.InvariantCulture)); + File.WriteAllText(_distCacheFilePath.Value, id.ToString(CultureInfo.InvariantCulture)); _lastId = id; } @@ -500,22 +499,8 @@ namespace Umbraco.Core.Sync + "/D" + AppDomain.CurrentDomain.Id // eg 22 + "] " + Guid.NewGuid().ToString("N").ToUpper(); // make it truly unique - /// - /// Gets the sync file path for the local server. - /// - /// The sync file path for the local server. - private string SyncFilePath + private static string GetDistCacheFilePath() { - get { return GetDistCacheFilePath(); } - } - - private string GetDistCacheFilePath() - { - //if it's already set then return it - we don't care about locking here - //if 2 threads do this at the same time it won't hurt - if (_distCacheFilePath != null) - return _distCacheFilePath; - var fileName = HttpRuntime.AppDomainAppId.ReplaceNonAlphanumericChars(string.Empty) + "-lastsynced.txt"; string distCacheFilePath; @@ -547,8 +532,7 @@ namespace Umbraco.Core.Sync if (Directory.Exists(folder) == false) Directory.CreateDirectory(folder); - _distCacheFilePath = distCacheFilePath; - return _distCacheFilePath; + return distCacheFilePath; } #endregion