diff --git a/src/OrchardCore/OrchardCore.Data.YesSql/Documents/DocumentStore.cs b/src/OrchardCore/OrchardCore.Data.YesSql/Documents/DocumentStore.cs index 2432ba6106e..fc44a016dc9 100644 --- a/src/OrchardCore/OrchardCore.Data.YesSql/Documents/DocumentStore.cs +++ b/src/OrchardCore/OrchardCore.Data.YesSql/Documents/DocumentStore.cs @@ -21,7 +21,6 @@ public class DocumentStore : IDocumentStore private DocumentStoreCommitFailureDelegate _afterCommitFailure; private bool _canceled; - private bool _committed; public DocumentStore(ISession session) { @@ -54,27 +53,10 @@ public DocumentStore(ISession session) return (false, loaded as T); } - T document = null; - - // For consistency checking a document may be queried after 'SaveChangesAsync()'. - if (_committed) - { - // So we create a new session to not get a cached version from 'YesSql'. - await using var session = _session.Store.CreateSession(); - document = await session.Query().FirstOrDefaultAsync(); - } - else + var document = await _session.Query().FirstOrDefaultAsync(); + if (document is not null) { - document = await _session.Query().FirstOrDefaultAsync(); - } - - if (document != null) - { - if (!_committed) - { - _session.Detach(document); - } - + _session.Detach(document); return (true, document); } @@ -131,7 +113,7 @@ public void AfterCommitFailure(DocumentStoreCommitFailureDelegate afterCommit /// public async Task CommitAsync() { - if (_session == null) + if (_session is null) { return; } @@ -140,10 +122,9 @@ public async Task CommitAsync() { await _session.SaveChangesAsync(); - _committed = true; _loaded.Clear(); - if (!_canceled && _afterCommitSuccess != null) + if (!_canceled && _afterCommitSuccess is not null) { foreach (var d in _afterCommitSuccess.GetInvocationList()) { @@ -153,7 +134,7 @@ public async Task CommitAsync() } catch (ConcurrencyException exception) { - if (_afterCommitFailure != null) + if (_afterCommitFailure is not null) { foreach (var d in _afterCommitFailure.GetInvocationList()) { diff --git a/src/OrchardCore/OrchardCore.Infrastructure/Documents/DocumentManager.cs b/src/OrchardCore/OrchardCore.Infrastructure/Documents/DocumentManager.cs index 58dba7b3948..cafdea6a7a8 100644 --- a/src/OrchardCore/OrchardCore.Infrastructure/Documents/DocumentManager.cs +++ b/src/OrchardCore/OrchardCore.Infrastructure/Documents/DocumentManager.cs @@ -52,7 +52,7 @@ public IDocumentStore DocumentStore get { var documentStore = (IDocumentStore)ShellScope.Get(DocumentStoreServiceType); - if (documentStore == null) + if (documentStore is null) { documentStore = (IDocumentStore)ShellScope.Services.GetRequiredService(DocumentStoreServiceType); ShellScope.Set(DocumentStoreServiceType, documentStore); @@ -78,7 +78,7 @@ public async Task GetOrCreateMutableAsync(Func> facto else { var volatileCache = ShellScope.Get(typeof(TDocument)); - if (volatileCache != null) + if (volatileCache is not null) { document = volatileCache; } @@ -133,7 +133,7 @@ public async Task GetOrCreateImmutableAsync(Func> fac }); } - if (document == null) + if (document is null) { var cacheable = true; @@ -186,6 +186,7 @@ public async Task UpdateAsync(TDocument document, Func afterUpd { await DocumentStore.UpdateAsync(document, async document => { + // A non volatile document can be invalidated. await InvalidateInternalAsync(document); if (afterUpdateAsync != null) @@ -204,9 +205,10 @@ await DocumentStore.UpdateAsync(document, async document => // But still update the shared cache after committing. DocumentStore.AfterCommitSuccess(async () => { - await InvalidateInternalAsync(document); + // A volatile document can't be invalidated. + await SetInternalAsync(document); - if (afterUpdateAsync != null) + if (afterUpdateAsync is not null) { await afterUpdateAsync(document); } @@ -239,7 +241,7 @@ private async Task GetInternalAsync(bool failover = false) id = _memoryCache.Get(_options.CacheIdKey); } - if (id == null) + if (id is null) { return null; } @@ -279,7 +281,7 @@ private async Task GetInternalAsync(bool failover = false) document = await GetFromDistributedCacheAsync(); - if (document == null) + if (document is null) { return null; } @@ -340,14 +342,7 @@ protected async Task SetInternalAsync(TDocument document, bool failover = false) if (stored.Identifier != document.Identifier) { - if (_isDistributed) - { - await _distributedCache.RemoveAsync(_options.CacheIdKey); - } - else - { - _memoryCache.Remove(_options.CacheIdKey); - } + await InvalidateInternalAsync(document); } } } diff --git a/src/OrchardCore/OrchardCore.Infrastructure/Documents/VolatileDocumentManager.cs b/src/OrchardCore/OrchardCore.Infrastructure/Documents/VolatileDocumentManager.cs index 275c575bdf3..edb20522515 100644 --- a/src/OrchardCore/OrchardCore.Infrastructure/Documents/VolatileDocumentManager.cs +++ b/src/OrchardCore/OrchardCore.Infrastructure/Documents/VolatileDocumentManager.cs @@ -63,7 +63,7 @@ public async Task UpdateAtomicAsync(Func> updateAsync, Func> updateAsync, Func Targets = new(); + public HashSet Targets = []; } } }