Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using FluentAssertions;
using NUnit.Framework;
using Speckle.Connectors.Common.Caching;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation;

namespace Speckle.Connectors.Common.Tests.Caching;

public class NullSendConversionCacheTests
{
[Test]
public void Store()
{
var cache = new NullSendConversionCache();
var projectId = "projectId";
var id = new Id("id");
var objectReference = new ObjectReference() { referencedId = "referencedId" };
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };

cache.StoreSendResult(projectId, convertedReferences);

cache.TryGetValue(projectId, id.Value, out ObjectReference? _).Should().BeFalse();

cache.ClearCache();

cache.TryGetValue(projectId, id.Value, out _).Should().BeFalse();
}

[Test]
public void Evict()
{
var cache = new NullSendConversionCache();
var projectId = "projectId";
var id = new Id("id");
var objectReference = new ObjectReference() { referencedId = "referencedId" };
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };

cache.StoreSendResult(projectId, convertedReferences);

cache.TryGetValue(projectId, id.Value, out ObjectReference? _).Should().BeFalse();

cache.EvictObjects([id.Value]);

cache.TryGetValue(projectId, id.Value, out _).Should().BeFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentAssertions;
using NUnit.Framework;
using Speckle.Connectors.Common.Caching;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation;

namespace Speckle.Connectors.Common.Tests.Caching;

public class SendConversionCacheTests
{
[Test]
public void Store()
{
var cache = new SendConversionCache();
var projectId = "projectId";
var id = new Id("id");
var objectReference = new ObjectReference() { referencedId = "referencedId" };
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };

cache.StoreSendResult(projectId, convertedReferences);
cache.Cache.Count.Should().Be(1);

cache.TryGetValue(projectId, id.Value, out ObjectReference? reference).Should().BeTrue();
reference.Should().Be(objectReference);

cache.ClearCache();
cache.Cache.Count.Should().Be(0);

cache.TryGetValue(projectId, id.Value, out _).Should().BeFalse();
}

[Test]
public void Evict()
{
var cache = new SendConversionCache();
var projectId = "projectId";
var id = new Id("id");
var objectReference = new ObjectReference() { referencedId = "referencedId" };
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };

cache.StoreSendResult(projectId, convertedReferences);
cache.Cache.Count.Should().Be(1);

cache.TryGetValue(projectId, id.Value, out ObjectReference? reference).Should().BeTrue();
reference.Should().Be(objectReference);

cache.EvictObjects([id.Value]);
cache.Cache.Count.Should().Be(0);
}
}
4 changes: 2 additions & 2 deletions Sdk/Speckle.Connectors.Common/Caching/ISendConversionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface ISendConversionCache
/// <para><b>Failure to do so correctly will result in cache poisoning and incorrect version creation (stale objects).</b></para>
/// </summary>
/// <param name="objectIds"></param>
public void EvictObjects(IEnumerable<string> objectIds);
public void ClearCache();
void EvictObjects(IEnumerable<string> objectIds);
void ClearCache();
bool TryGetValue(string projectId, string applicationId, [NotNullWhen(true)] out ObjectReference? objectReference);
}
4 changes: 1 addition & 3 deletions Sdk/Speckle.Connectors.Common/Caching/SendConversionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace Speckle.Connectors.Common.Caching;
///<inheritdoc/>
public class SendConversionCache : ISendConversionCache
{
public SendConversionCache() { }

private Dictionary<(string applicationId, string projectId), ObjectReference> Cache { get; set; } = new(); // NOTE: as this dude's accessed from potentially more operations at the same time, it might be safer to bless him as a concurrent dictionary.
public Dictionary<(string applicationId, string projectId), ObjectReference> Cache { get; set; } = new(); // NOTE: as this dude's accessed from potentially more operations at the same time, it might be safer to bless him as a concurrent dictionary.

public void StoreSendResult(string projectId, IReadOnlyDictionary<Id, ObjectReference> convertedReferences)
{
Expand Down
Loading