Skip to content

Commit

Permalink
path lookup caching (stop multiple db lookups, speedup content serial…
Browse files Browse the repository at this point in the history
…ization)
  • Loading branch information
Kevin Jump committed Dec 6, 2019
1 parent d625e61 commit 1fb0eaf
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions uSync8.ContentEdition/Serializers/ContentSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public abstract class ContentSerializerBase<TObject> : SyncTreeSerializerBase<TO
protected UmbracoObjectTypes umbracoObjectType;
protected SyncValueMapperCollection syncMappers;

protected Dictionary<string, string> pathCache;

public ContentSerializerBase(
IEntityService entityService,
ILogger logger,
Expand All @@ -31,6 +33,8 @@ public ContentSerializerBase(
{
this.umbracoObjectType = umbracoObjectType;
this.syncMappers = syncMappers;

this.pathCache = new Dictionary<string, string>();
}

protected virtual XElement InitializeNode(TObject item, string typeName)
Expand Down Expand Up @@ -184,6 +188,9 @@ protected Attempt<TObject> DeserializeName(TObject item, XElement node)
}
}

// clear the path cache of anything with this id.
pathCache.RemoveAll(x => x.Key.Contains(item.Id.ToString()));

return Attempt.Succeed(item);
}

Expand Down Expand Up @@ -293,15 +300,19 @@ protected virtual string GetItemPath(TObject item)

protected virtual string GetItemPath(IEntitySlim item)
{
// path caching, stops us looking up the same path all the time.
if (pathCache.ContainsKey(item.Path)) return pathCache[item.Path];

var path = "";
if (item.ParentId != -1)
{
var parent = entityService.Get(item.ParentId);
var parent = entityService.Get(item.ParentId);
if (parent != null)
path += GetItemPath(parent);
}

return path += "/" + item.Name.ToSafeAlias();
pathCache[item.Path] = path + "/" + item.Name.ToSafeAlias();
return pathCache[item.Path];
}

#region Finders
Expand Down

0 comments on commit 1fb0eaf

Please sign in to comment.