Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #12544: Make UpdateAtomic Delegates distinct #12545

Merged
merged 3 commits into from
Oct 9, 2022
Merged
Changes from all commits
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
@@ -1,5 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
Expand All @@ -15,7 +15,8 @@ namespace OrchardCore.Documents
/// <summary>
/// A <see cref="DocumentManager{TDocument}"/> using a multi level cache but without any persistent storage.
/// </summary>
public class VolatileDocumentManager<TDocument> : DocumentManager<TDocument>, IVolatileDocumentManager<TDocument> where TDocument : class, IDocument, new()
public class VolatileDocumentManager<TDocument> : DocumentManager<TDocument>, IVolatileDocumentManager<TDocument>
where TDocument : class, IDocument, new()
{
private readonly IDistributedLock _distributedLock;
private readonly ILogger _logger;
Expand Down Expand Up @@ -49,23 +50,26 @@ public async Task UpdateAtomicAsync(Func<Task<TDocument>> updateAsync, Func<TDoc
await DocumentStore.CancelAsync();

_logger.LogError("Can't update the '{DocumentName}' if not able to access the distributed cache", typeof(TDocument).Name);

throw;
}
}

var delegates = ShellScope.GetOrCreateFeature<UpdateDelegates>();
if (delegates.UpdateDelegateAsync == null ||
!delegates.UpdateDelegateAsync.GetInvocationList().Contains(updateAsync))

var updateDelegate = new UpdateDelegate(updateAsync);
if (delegates.Targets.Add(updateDelegate.Target))
{
delegates.UpdateDelegateAsync += () => updateAsync();
delegates.UpdateDelegateAsync += updateDelegate;
}

if (afterUpdateAsync != null &&
(delegates.AfterUpdateDelegateAsync == null ||
!delegates.AfterUpdateDelegateAsync.GetInvocationList().Contains(afterUpdateAsync)))
if (afterUpdateAsync != null)
{
delegates.AfterUpdateDelegateAsync += document => afterUpdateAsync(document);
var afterUpdateDelegate = new AfterUpdateDelegate(afterUpdateAsync);
if (delegates.Targets.Add(afterUpdateDelegate.Target))
{
delegates.AfterUpdateDelegateAsync += afterUpdateDelegate;
}
}

DocumentStore.AfterCommitSuccess<TDocument>(async () =>
Expand All @@ -88,6 +92,11 @@ public async Task UpdateAtomicAsync(Func<Task<TDocument>> updateAsync, Func<TDoc
document = await ((UpdateDelegate)d)();
}

if (document == null)
{
return;
}

document.Identifier ??= IdGenerator.GenerateId();

await SetInternalAsync(document);
Expand All @@ -102,10 +111,11 @@ public async Task UpdateAtomicAsync(Func<Task<TDocument>> updateAsync, Func<TDoc
});
}

private class UpdateDelegates
private sealed class UpdateDelegates
{
public UpdateDelegate UpdateDelegateAsync;
public AfterUpdateDelegate AfterUpdateDelegateAsync;
public HashSet<object> Targets = new();
}
}
}