Skip to content

Commit

Permalink
Fixes #2997 - ContentService returns outdated result
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed Sep 27, 2019
1 parent cf36268 commit d4afc10
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
6 changes: 4 additions & 2 deletions src/Umbraco.Tests/Cache/RefresherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ public class RefreshersTests
[Test]
public void MediaCacheRefresherCanDeserializeJsonPayload()
{
var source = new[] { new MediaCacheRefresher.JsonPayload(1234, TreeChangeTypes.None) };
var source = new[] { new MediaCacheRefresher.JsonPayload(1234, Guid.NewGuid(), TreeChangeTypes.None) };
var json = JsonConvert.SerializeObject(source);
var payload = JsonConvert.DeserializeObject<MediaCacheRefresher.JsonPayload[]>(json);
Assert.AreEqual(source[0].Id, payload[0].Id);
Assert.AreEqual(source[0].Key, payload[0].Key);
Assert.AreEqual(source[0].ChangeTypes, payload[0].ChangeTypes);
}

[Test]
public void ContentCacheRefresherCanDeserializeJsonPayload()
{
var source = new[] { new ContentCacheRefresher.JsonPayload(1234, TreeChangeTypes.None) };
var source = new[] { new ContentCacheRefresher.JsonPayload(1234, Guid.NewGuid(), TreeChangeTypes.None) };
var json = JsonConvert.SerializeObject(source);
var payload = JsonConvert.DeserializeObject<ContentCacheRefresher.JsonPayload[]>(json);
Assert.AreEqual(source[0].Id, payload[0].Id);
Assert.AreEqual(source[0].Key, payload[0].Key);
Assert.AreEqual(source[0].ChangeTypes, payload[0].ChangeTypes);
}

Expand Down
32 changes: 16 additions & 16 deletions src/Umbraco.Tests/PublishedContent/NuCacheChildrenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public void MoveToRootTest()
};

// notify
_snapshotService.Notify(new[] { new ContentCacheRefresher.JsonPayload(10, TreeChangeTypes.RefreshBranch) }, out _, out _);
_snapshotService.Notify(new[] { new ContentCacheRefresher.JsonPayload(10, Guid.Empty, TreeChangeTypes.RefreshBranch) }, out _, out _);

// changes that *I* make are immediately visible on the current snapshot
var documents = snapshot.Content.GetAtRoot().ToArray();
Expand Down Expand Up @@ -500,7 +500,7 @@ public void MoveFromRootTest()
};

// notify
_snapshotService.Notify(new[] { new ContentCacheRefresher.JsonPayload(1, TreeChangeTypes.RefreshBranch) }, out _, out _);
_snapshotService.Notify(new[] { new ContentCacheRefresher.JsonPayload(1, Guid.Empty, TreeChangeTypes.RefreshBranch) }, out _, out _);

// changes that *I* make are immediately visible on the current snapshot
var documents = snapshot.Content.GetAtRoot().ToArray();
Expand Down Expand Up @@ -580,7 +580,7 @@ public void ReOrderTest()
};

// notify
_snapshotService.Notify(new[] { new ContentCacheRefresher.JsonPayload(kit.Node.ParentContentId, TreeChangeTypes.RefreshBranch) }, out _, out _);
_snapshotService.Notify(new[] { new ContentCacheRefresher.JsonPayload(kit.Node.ParentContentId, Guid.Empty, TreeChangeTypes.RefreshBranch) }, out _, out _);

// changes that *I* make are immediately visible on the current snapshot
var documents = snapshot.Content.GetById(kit.Node.ParentContentId).Children().ToArray();
Expand Down Expand Up @@ -677,8 +677,8 @@ public void MoveTest()
_snapshotService.Notify(new[]
{
// removal must come first
new ContentCacheRefresher.JsonPayload(2, TreeChangeTypes.RefreshBranch),
new ContentCacheRefresher.JsonPayload(1, TreeChangeTypes.RefreshBranch)
new ContentCacheRefresher.JsonPayload(2, Guid.Empty, TreeChangeTypes.RefreshBranch),
new ContentCacheRefresher.JsonPayload(1, Guid.Empty, TreeChangeTypes.RefreshBranch)
}, out _, out _);

// changes that *I* make are immediately visible on the current snapshot
Expand Down Expand Up @@ -873,9 +873,9 @@ public void RemoveTest()
// notify
_snapshotService.Notify(new[]
{
new ContentCacheRefresher.JsonPayload(3, TreeChangeTypes.Remove), // remove last
new ContentCacheRefresher.JsonPayload(5, TreeChangeTypes.Remove), // remove middle
new ContentCacheRefresher.JsonPayload(9, TreeChangeTypes.Remove), // remove first
new ContentCacheRefresher.JsonPayload(3, Guid.Empty, TreeChangeTypes.Remove), // remove last
new ContentCacheRefresher.JsonPayload(5, Guid.Empty, TreeChangeTypes.Remove), // remove middle
new ContentCacheRefresher.JsonPayload(9, Guid.Empty, TreeChangeTypes.Remove), // remove first
}, out _, out _);

documents = snapshot.Content.GetAtRoot().ToArray();
Expand All @@ -890,9 +890,9 @@ public void RemoveTest()
// notify
_snapshotService.Notify(new[]
{
new ContentCacheRefresher.JsonPayload(1, TreeChangeTypes.Remove), // remove first
new ContentCacheRefresher.JsonPayload(8, TreeChangeTypes.Remove), // remove
new ContentCacheRefresher.JsonPayload(7, TreeChangeTypes.Remove), // remove
new ContentCacheRefresher.JsonPayload(1, Guid.Empty, TreeChangeTypes.Remove), // remove first
new ContentCacheRefresher.JsonPayload(8, Guid.Empty, TreeChangeTypes.Remove), // remove
new ContentCacheRefresher.JsonPayload(7, Guid.Empty, TreeChangeTypes.Remove), // remove
}, out _, out _);

documents = snapshot.Content.GetAtRoot().ToArray();
Expand Down Expand Up @@ -922,8 +922,8 @@ public void UpdateTest()
// notify
_snapshotService.Notify(new[]
{
new ContentCacheRefresher.JsonPayload(1, TreeChangeTypes.RefreshBranch),
new ContentCacheRefresher.JsonPayload(2, TreeChangeTypes.RefreshNode),
new ContentCacheRefresher.JsonPayload(1, Guid.Empty, TreeChangeTypes.RefreshBranch),
new ContentCacheRefresher.JsonPayload(2, Guid.Empty, TreeChangeTypes.RefreshNode),
}, out _, out _);

documents = snapshot.Content.GetAtRoot().ToArray();
Expand Down Expand Up @@ -979,7 +979,7 @@ IEnumerable<ContentNodeKit> GetKits()

_snapshotService.Notify(new[]
{
new ContentCacheRefresher.JsonPayload(2, TreeChangeTypes.Remove)
new ContentCacheRefresher.JsonPayload(2, Guid.Empty, TreeChangeTypes.Remove)
}, out _, out _);

parentNodes = contentStore.Test.GetValues(1);
Expand Down Expand Up @@ -1038,7 +1038,7 @@ IEnumerable<ContentNodeKit> GetKits()

_snapshotService.Notify(new[]
{
new ContentCacheRefresher.JsonPayload(3, TreeChangeTypes.Remove) //remove middle child
new ContentCacheRefresher.JsonPayload(3, Guid.Empty, TreeChangeTypes.Remove) //remove middle child
}, out _, out _);

Assert.AreEqual(2, contentStore.Test.LiveGen);
Expand Down Expand Up @@ -1113,7 +1113,7 @@ IEnumerable<ContentNodeKit> GetKits()

_snapshotService.Notify(new[]
{
new ContentCacheRefresher.JsonPayload(3, TreeChangeTypes.RefreshBranch) //remove middle child
new ContentCacheRefresher.JsonPayload(3, Guid.Empty, TreeChangeTypes.RefreshBranch) //remove middle child
}, out _, out _);

Assert.AreEqual(2, contentStore.Test.LiveGen);
Expand Down
8 changes: 6 additions & 2 deletions src/Umbraco.Web/Cache/ContentCacheRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public override void Refresh(JsonPayload[] payloads)

foreach (var payload in payloads.Where(x => x.Id != default))
{
//By INT Id
isolatedCache.Clear(RepositoryCacheKeys.GetKey<IContent>(payload.Id));
//By GUID Key
isolatedCache.Clear(RepositoryCacheKeys.GetKey<IContent>(payload.Key));

_idkMap.ClearCache(payload.Id);

Expand Down Expand Up @@ -137,14 +140,15 @@ public override void Remove(int id)

public class JsonPayload
{
public JsonPayload(int id, TreeChangeTypes changeTypes)
public JsonPayload(int id, Guid? key, TreeChangeTypes changeTypes)
{
Id = id;
Key = key;
ChangeTypes = changeTypes;
}

public int Id { get; }

public Guid? Key { get; }
public TreeChangeTypes ChangeTypes { get; }
}

Expand Down
8 changes: 4 additions & 4 deletions src/Umbraco.Web/Cache/DistributedCacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void RemoveDataTypeCache(this DistributedCache dc, IDataType dataT

public static void RefreshAllContentCache(this DistributedCache dc)
{
var payloads = new[] { new ContentCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) };
var payloads = new[] { new ContentCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) };

// note: refresh all content cache does refresh content types too
dc.RefreshByPayload(ContentCacheRefresher.UniqueId, payloads);
Expand All @@ -117,7 +117,7 @@ public static void RefreshContentCache(this DistributedCache dc, TreeChange<ICon
if (changes.Length == 0) return;

var payloads = changes
.Select(x => new ContentCacheRefresher.JsonPayload(x.Item.Id, x.ChangeTypes));
.Select(x => new ContentCacheRefresher.JsonPayload(x.Item.Id, x.Item.Key, x.ChangeTypes));

dc.RefreshByPayload(ContentCacheRefresher.UniqueId, payloads);
}
Expand Down Expand Up @@ -157,7 +157,7 @@ public static void RemoveMemberGroupCache(this DistributedCache dc, int memberGr

public static void RefreshAllMediaCache(this DistributedCache dc)
{
var payloads = new[] { new MediaCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) };
var payloads = new[] { new MediaCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) };

// note: refresh all media cache does refresh content types too
dc.RefreshByPayload(MediaCacheRefresher.UniqueId, payloads);
Expand All @@ -168,7 +168,7 @@ public static void RefreshMediaCache(this DistributedCache dc, TreeChange<IMedia
if (changes.Length == 0) return;

var payloads = changes
.Select(x => new MediaCacheRefresher.JsonPayload(x.Item.Id, x.ChangeTypes));
.Select(x => new MediaCacheRefresher.JsonPayload(x.Item.Id, x.Item.Key, x.ChangeTypes));

dc.RefreshByPayload(MediaCacheRefresher.UniqueId, payloads);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Umbraco.Web/Cache/MediaCacheRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public override void Refresh(JsonPayload[] payloads)
// it *was* done for each pathId but really that does not make sense
// only need to do it for the current media
mediaCache.Result.Clear(RepositoryCacheKeys.GetKey<IMedia>(payload.Id));
mediaCache.Result.Clear(RepositoryCacheKeys.GetKey<IMedia>(payload.Key));

// remove those that are in the branch
if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.RefreshBranch | TreeChangeTypes.Remove))
Expand Down Expand Up @@ -104,14 +105,15 @@ public override void Remove(int id)

public class JsonPayload
{
public JsonPayload(int id, TreeChangeTypes changeTypes)
public JsonPayload(int id, Guid? key, TreeChangeTypes changeTypes)
{
Id = id;
Key = key;
ChangeTypes = changeTypes;
}

public int Id { get; }

public Guid? Key { get; }
public TreeChangeTypes ChangeTypes { get; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static DatabaseServerMessengerOptions GetDefaultOptions(IFactory factory)
// note: refresh all content & media caches does refresh content types too
var svc = Current.PublishedSnapshotService;
svc.Notify(new[] { new DomainCacheRefresher.JsonPayload(0, DomainChangeTypes.RefreshAll) });
svc.Notify(new[] { new ContentCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) }, out _, out _);
svc.Notify(new[] { new MediaCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) }, out _);
svc.Notify(new[] { new ContentCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) }, out _, out _);
svc.Notify(new[] { new MediaCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) }, out _);
},

//rebuild indexes if the server is not synced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,8 @@ public override void Notify(ContentTypeCacheRefresher.JsonPayload[] payloads)
using (_contentStore.GetScopedWriteLock(_scopeProvider))
using (_mediaStore.GetScopedWriteLock(_scopeProvider))
{
NotifyLocked(new[] { new ContentCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) }, out var draftChanged, out var publishedChanged);
NotifyLocked(new[] { new MediaCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) }, out var anythingChanged);
NotifyLocked(new[] { new ContentCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) }, out var draftChanged, out var publishedChanged);
NotifyLocked(new[] { new MediaCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) }, out var anythingChanged);
}
}

Expand Down

0 comments on commit d4afc10

Please sign in to comment.