From 9d23ae652028b95a9c8036b588570869590654d6 Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Mon, 16 Sep 2024 09:26:48 +0100 Subject: [PATCH] Dictionary checks. --- .../SyncHandlers/Handlers/LanguageHandler.cs | 10 +++++----- .../Services/uSyncManagmenetCache.cs | 7 +------ .../Mapping/SyncValueMapperCollection.cs | 13 +++++-------- .../SyncContainerSerializerBase.cs | 18 ++++++++++++------ 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/uSync.BackOffice/SyncHandlers/Handlers/LanguageHandler.cs b/uSync.BackOffice/SyncHandlers/Handlers/LanguageHandler.cs index f185c972..3dc4dfc7 100644 --- a/uSync.BackOffice/SyncHandlers/Handlers/LanguageHandler.cs +++ b/uSync.BackOffice/SyncHandlers/Handlers/LanguageHandler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Xml.Linq; using Microsoft.Extensions.Logging; @@ -85,7 +86,7 @@ protected override IEnumerable GetImportFiles(string folder) try { - Dictionary ordered = new Dictionary(); + Dictionary ordered = []; foreach (var file in files) { var node = XElement.Load(file); @@ -181,7 +182,7 @@ public override void Handle(SavingNotification notification) // if (item.Id == 0) { - newLanguages[item.IsoCode] = item.CultureName; + newLanguages.TryAdd(item.IsoCode, item.CultureName); // is new, we want to set this as a flag, so we don't do the full content save.n // newLanguages.Add(item.IsoCode); } @@ -196,10 +197,9 @@ public override void Handle(SavedNotification notification) foreach (var item in notification.SavedEntities) { bool newItem = false; - if (newLanguages.Count > 0 && newLanguages.ContainsKey(item.IsoCode)) - { + + if (newLanguages.TryRemove(item.IsoCode, out var _)) { newItem = true; - newLanguages.TryRemove(item.IsoCode, out string? name); } var targetFolders = GetDefaultHandlerFolders(); diff --git a/uSync.Backoffice.Management.Api/Services/uSyncManagmenetCache.cs b/uSync.Backoffice.Management.Api/Services/uSyncManagmenetCache.cs index 5e4f9d26..f720a446 100644 --- a/uSync.Backoffice.Management.Api/Services/uSyncManagmenetCache.cs +++ b/uSync.Backoffice.Management.Api/Services/uSyncManagmenetCache.cs @@ -17,12 +17,7 @@ public Guid GetNewCacheId() => Guid.NewGuid(); public List GetCachedActions(Guid id) - { - if (_actionCache.TryGetValue(id, out var actions)) - return actions; - - return new List(); - } + => _actionCache.TryGetValue(id, out var actions) ? actions : []; public bool IsValid(Guid id) => true; diff --git a/uSync.Core/Mapping/SyncValueMapperCollection.cs b/uSync.Core/Mapping/SyncValueMapperCollection.cs index 4657dc94..e79fb956 100644 --- a/uSync.Core/Mapping/SyncValueMapperCollection.cs +++ b/uSync.Core/Mapping/SyncValueMapperCollection.cs @@ -1,4 +1,6 @@ -using Umbraco.Cms.Core.Composing; +using System.Collections.Concurrent; + +using Umbraco.Cms.Core.Composing; using Umbraco.Extensions; using uSync.Core.Cache; @@ -9,7 +11,7 @@ namespace uSync.Core.Mapping; public class SyncValueMapperCollection : BuilderCollectionBase { - private readonly Dictionary _customMappings; + private readonly ConcurrentDictionary _customMappings; public SyncEntityCache EntityCache { get; private set; } @@ -118,12 +120,7 @@ private static string GetSafeValue(object value) /// looks up the alias for a mapper (replacing it from settings if need be) /// private string GetMapperAlias(string alias) - { - if (_customMappings.ContainsKey(alias.ToLower())) - return _customMappings[alias.ToLower()]; - - return alias; - } + => _customMappings.TryGetValue(alias.ToLower(), out var mappedAlias) ? mappedAlias : alias; } public class SyncValueMapperCollectionBuilder diff --git a/uSync.Core/Serialization/SyncContainerSerializerBase.cs b/uSync.Core/Serialization/SyncContainerSerializerBase.cs index 93331b15..f988f9c2 100644 --- a/uSync.Core/Serialization/SyncContainerSerializerBase.cs +++ b/uSync.Core/Serialization/SyncContainerSerializerBase.cs @@ -1,4 +1,5 @@ -using System.Web; +using System.Collections.Concurrent; +using System.Web; using System.Xml.Linq; using Microsoft.Extensions.Logging; @@ -128,12 +129,17 @@ protected override SyncAttempt ProcessDelete(Guid key, string alias, Se if (item.ParentId <= 0) return default; // return GetFolderNode(GetContainers(item)); - if (!_folderCache.ContainsKey(item.ParentId)) + if (_folderCache.TryGetValue(item.ParentId, out var folder) && folder is not null) + return folder; + + var node = GetFolderNode(GetContainers(item)); + if (node is not null) { - var node = GetFolderNode(GetContainers(item)); - if (node is not null) _folderCache[item.ParentId] = node; + _folderCache.TryAdd(item.ParentId, node); + return folder; } - return _folderCache[item.ParentId]; + + return default; } protected abstract IEnumerable GetContainers(TObject item); @@ -224,7 +230,7 @@ protected override SyncAttempt ProcessDelete(Guid key, string alias, Se /// /// only used on serialization, allows us to only build the folder path for a set of containers once. /// - private Dictionary _folderCache = []; + private ConcurrentDictionary _folderCache = []; private void ClearFolderCache() => _folderCache = [];