* V16: Cache Version Mechanism (#19747) * Add RepositoryCacheVersion table * Add repository * Add Cache version lock * Add GetAll method to repository * Add RepositoryCacheVersionService * Remember to add lock in data creator * Work my way out of constructor hell This is why we use DI folks. 🤦 * Add checks to specific cache policies * Fix migration * Add to schema creator * Fix database access * Initialize the cache version on in memory miss * Make cache version service internal * Add tests * Apply suggestions from code review Co-authored-by: Andy Butland <abutland73@gmail.com> * Add missing obsoletions * Prefer full name --------- Co-authored-by: Andy Butland <abutland73@gmail.com> * fixed merge * V16/feature/move last synced id to db (#19884) * Foundation work for moving last synced id * register manager and repo in dependency injection * Fixing to make tests work * Replacing the use of the old LastSyncedFileManager.cs with the new LastSyncedManager.cs * Testing to delete out of sync id and old entries * changing some stuff to please the reviewer. * Inverted saving methods id check and fixed documentation mishaps * Loadbalancing: Add Cache Sync service to allow us to roll forward isolated caches when backoffice is load balanced. (#20398) * Split cache refreshers into internal and external caches * Add obsolete constructor for CacheInstructionsPruningJob * Add xml docs * Move lastID management into CacheInstructionService * Cache last synced ids in memory * Lock when processing instructions * Sync caches when out of sync * Fix constructors for ICacheSyncService * Cache version on request * Register caches as synced when instructions are processed * Rename CacheVersionAccessor to IRepositoryCacheVersionAccessor * Set caches as synced before actually syncing the caches * Set caches as synced before syncing, within scope, this should also lock the cache version from being written to whilst updating caches * Only check version for backoffice requests * Clear request cache when caches are syned * Default to using NOOP cache version service * Don't generate local identity in database server messenger anymore * Fix ambiguous constructor * Add helper method to switch to load balanced isolated caches * Fix LastSyncedManagerTests * Fix RepositoryCacheVersionServiceTests * Fix DefaultCachePolicyTests * Use correct constructor in FullDataSetRepositoryCachePolicy * Minor cleanup * Add XML docs * Add more xml docs * Apply suggestions from code review Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> --------- Co-authored-by: Zeegaan <skrivdetud@gmail.com> Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> * Fix migration plan * fix tests * Fix integration tests * Fix changes from github review * Move premigrations to v17 * Make lock constantws sequential * Fix comment * Make IRepositoryCacheVersionService and ICacheSyncService protected on EntityRepositoryBase --------- Co-authored-by: Andy Butland <abutland73@gmail.com> Co-authored-by: Nicklas Kramer <nik@umbraco.dk> Co-authored-by: NillasKA <kramernicklas@gmail.com> Co-authored-by: Zeegaan <skrivdetud@gmail.com> Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
223 lines
7.6 KiB
C#
223 lines
7.6 KiB
C#
// Copyright (c) Umbraco.
|
|
// See LICENSE for more details.
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using Umbraco.Cms.Core.Cache;
|
|
using Umbraco.Cms.Core.Collections;
|
|
using Umbraco.Cms.Core.Models;
|
|
using Umbraco.Cms.Core.Scoping;
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
|
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;
|
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Cache;
|
|
|
|
[TestFixture]
|
|
public class FullDataSetCachePolicyTests
|
|
{
|
|
private IScopeAccessor DefaultAccessor
|
|
{
|
|
get
|
|
{
|
|
var accessor = new Mock<IScopeAccessor>();
|
|
var scope = new Mock<IScope>();
|
|
scope.Setup(x => x.RepositoryCacheMode).Returns(RepositoryCacheMode.Default);
|
|
accessor.Setup(x => x.AmbientScope).Returns(scope.Object);
|
|
return accessor.Object;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void Caches_Single()
|
|
{
|
|
AuditItem[] getAll =
|
|
{
|
|
new(1, AuditType.Copy, 123, "test", "blah"),
|
|
new(2, AuditType.Copy, 123, "test", "blah2"),
|
|
};
|
|
|
|
var isCached = false;
|
|
var cache = new Mock<IAppPolicyCache>();
|
|
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>()))
|
|
.Callback(() => isCached = true);
|
|
|
|
var policy =
|
|
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
|
|
var unused = policy.Get(1, id => new AuditItem(1, AuditType.Copy, 123, "test", "blah"), ids => getAll);
|
|
Assert.IsTrue(isCached);
|
|
}
|
|
|
|
[Test]
|
|
public void Get_Single_From_Cache()
|
|
{
|
|
AuditItem[] getAll =
|
|
{
|
|
new(1, AuditType.Copy, 123, "test", "blah"), new(2, AuditType.Copy, 123, "test", "blah2"),
|
|
};
|
|
|
|
var cache = new Mock<IAppPolicyCache>();
|
|
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(new AuditItem(1, AuditType.Copy, 123, "test", "blah"));
|
|
|
|
var defaultPolicy =
|
|
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
|
|
var found = defaultPolicy.Get(1, id => null, ids => getAll);
|
|
Assert.IsNotNull(found);
|
|
}
|
|
|
|
[Test]
|
|
public void Get_All_Caches_Empty_List()
|
|
{
|
|
var getAll = new AuditItem[] { };
|
|
|
|
var cached = new List<string>();
|
|
|
|
IList list = null;
|
|
|
|
var cache = new Mock<IAppPolicyCache>();
|
|
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>()))
|
|
.Callback((string cacheKey, Func<object> o, TimeSpan? t, bool b) =>
|
|
{
|
|
cached.Add(cacheKey);
|
|
|
|
list = o() as IList;
|
|
});
|
|
|
|
// Return null if this is the first pass.
|
|
cache.Setup(x => x.Get(It.IsAny<string>()))
|
|
.Returns(() => cached.Any() ? new DeepCloneableList<AuditItem>(ListCloneBehavior.CloneOnce) : null);
|
|
|
|
var policy =
|
|
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
|
|
var found = policy.GetAll(new object[] { }, ids => getAll);
|
|
|
|
Assert.AreEqual(1, cached.Count);
|
|
Assert.IsNotNull(list);
|
|
|
|
// Do it again, ensure that its coming from the cache!
|
|
policy = new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
|
|
found = policy.GetAll(new object[] { }, ids => getAll);
|
|
|
|
Assert.AreEqual(1, cached.Count);
|
|
Assert.IsNotNull(list);
|
|
}
|
|
|
|
[Test]
|
|
public void Get_All_Caches_As_Single_List()
|
|
{
|
|
AuditItem[] getAll =
|
|
{
|
|
new(1, AuditType.Copy, 123, "test", "blah"),
|
|
new(2, AuditType.Copy, 123, "test", "blah2"),
|
|
};
|
|
|
|
var cached = new List<string>();
|
|
IList list = null;
|
|
|
|
var cache = new Mock<IAppPolicyCache>();
|
|
cache.Setup(x => x.Insert(It.IsAny<string>(), It.IsAny<Func<object>>(), It.IsAny<TimeSpan?>(), It.IsAny<bool>()))
|
|
.Callback((string cacheKey, Func<object> o, TimeSpan? t, bool b) =>
|
|
{
|
|
cached.Add(cacheKey);
|
|
|
|
list = o() as IList;
|
|
});
|
|
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(new AuditItem[] { });
|
|
|
|
var defaultPolicy =
|
|
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
|
|
var found = defaultPolicy.GetAll(new object[] { }, ids => getAll);
|
|
|
|
Assert.AreEqual(1, cached.Count);
|
|
Assert.IsNotNull(list);
|
|
}
|
|
|
|
[Test]
|
|
public void Get_All_Without_Ids_From_Cache()
|
|
{
|
|
AuditItem[] getAll = { null };
|
|
|
|
var cache = new Mock<IAppPolicyCache>();
|
|
|
|
cache.Setup(x => x.Get(It.IsAny<string>())).Returns(() =>
|
|
new DeepCloneableList<AuditItem>(ListCloneBehavior.CloneOnce)
|
|
{
|
|
new(1, AuditType.Copy, 123, "test", "blah"),
|
|
new(2, AuditType.Copy, 123, "test", "blah2"),
|
|
});
|
|
|
|
var defaultPolicy =
|
|
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
|
|
var found = defaultPolicy.GetAll(new object[] { }, ids => getAll);
|
|
Assert.AreEqual(2, found.Length);
|
|
}
|
|
|
|
[Test]
|
|
public void If_CreateOrUpdate_Throws_Cache_Is_Removed()
|
|
{
|
|
AuditItem[] getAll =
|
|
{
|
|
new(1, AuditType.Copy, 123, "test", "blah"),
|
|
new(2, AuditType.Copy, 123, "test", "blah2"),
|
|
};
|
|
|
|
var cacheCleared = false;
|
|
var cache = new Mock<IAppPolicyCache>();
|
|
cache.Setup(x => x.Clear(It.IsAny<string>()))
|
|
.Callback(() => cacheCleared = true);
|
|
|
|
var defaultPolicy =
|
|
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
try
|
|
{
|
|
defaultPolicy.Update(new AuditItem(1, AuditType.Copy, 123, "test", "blah"), item => throw new Exception("blah!"));
|
|
}
|
|
catch
|
|
{
|
|
// We need this catch or nunit throws up.
|
|
}
|
|
finally
|
|
{
|
|
Assert.IsTrue(cacheCleared);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void If_Removes_Throws_Cache_Is_Removed()
|
|
{
|
|
AuditItem[] getAll =
|
|
{
|
|
new(1, AuditType.Copy, 123, "test", "blah"),
|
|
new(2, AuditType.Copy, 123, "test", "blah2"),
|
|
};
|
|
|
|
var cacheCleared = false;
|
|
var cache = new Mock<IAppPolicyCache>();
|
|
cache.Setup(x => x.Clear(It.IsAny<string>()))
|
|
.Callback(() => cacheCleared = true);
|
|
|
|
var defaultPolicy =
|
|
new FullDataSetRepositoryCachePolicy<AuditItem, object>(cache.Object, DefaultAccessor, new SingleServerCacheVersionService(), Mock.Of<ICacheSyncService>(), item => item.Id, false);
|
|
try
|
|
{
|
|
defaultPolicy.Delete(new AuditItem(1, AuditType.Copy, 123, "test", "blah"), item => throw new Exception("blah!"));
|
|
}
|
|
catch
|
|
{
|
|
// We need this catch or nunit throws up.
|
|
}
|
|
finally
|
|
{
|
|
Assert.IsTrue(cacheCleared);
|
|
}
|
|
}
|
|
}
|