Skip to content

Commit

Permalink
Dictionary checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Sep 16, 2024
1 parent 089e989 commit 9d23ae6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
10 changes: 5 additions & 5 deletions uSync.BackOffice/SyncHandlers/Handlers/LanguageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +86,7 @@ protected override IEnumerable<string> GetImportFiles(string folder)

try
{
Dictionary<string, string> ordered = new Dictionary<string, string>();
Dictionary<string, string> ordered = [];
foreach (var file in files)
{
var node = XElement.Load(file);
Expand Down Expand Up @@ -181,7 +182,7 @@ public override void Handle(SavingNotification<ILanguage> 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);
}
Expand All @@ -196,10 +197,9 @@ public override void Handle(SavedNotification<ILanguage> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ public Guid GetNewCacheId()
=> Guid.NewGuid();

public List<uSyncAction> GetCachedActions(Guid id)
{
if (_actionCache.TryGetValue(id, out var actions))
return actions;

return new List<uSyncAction>();
}
=> _actionCache.TryGetValue(id, out var actions) ? actions : [];

public bool IsValid(Guid id)
=> true;
Expand Down
13 changes: 5 additions & 8 deletions uSync.Core/Mapping/SyncValueMapperCollection.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +11,7 @@ namespace uSync.Core.Mapping;
public class SyncValueMapperCollection
: BuilderCollectionBase<ISyncMapper>
{
private readonly Dictionary<string, string> _customMappings;
private readonly ConcurrentDictionary<string, string> _customMappings;

public SyncEntityCache EntityCache { get; private set; }

Expand Down Expand Up @@ -118,12 +120,7 @@ private static string GetSafeValue(object value)
/// looks up the alias for a mapper (replacing it from settings if need be)
/// </summary>
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
Expand Down
18 changes: 12 additions & 6 deletions uSync.Core/Serialization/SyncContainerSerializerBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Web;
using System.Collections.Concurrent;
using System.Web;
using System.Xml.Linq;

using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -128,12 +129,17 @@ protected override SyncAttempt<TObject> 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<EntityContainer> GetContainers(TObject item);
Expand Down Expand Up @@ -224,7 +230,7 @@ protected override SyncAttempt<TObject> ProcessDelete(Guid key, string alias, Se
/// <remarks>
/// only used on serialization, allows us to only build the folder path for a set of containers once.
/// </remarks>
private Dictionary<int, XElement> _folderCache = [];
private ConcurrentDictionary<int, XElement> _folderCache = [];

private void ClearFolderCache()
=> _folderCache = [];
Expand Down

0 comments on commit 9d23ae6

Please sign in to comment.