Skip to content

Commit

Permalink
Invalidate cache on updating store (#14612)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkech authored Nov 2, 2023
1 parent 23e1a4b commit c8f1894
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public async Task UpdateAsync(TDocument document, Func<TDocument, Task> afterUpd
{
await DocumentStore.UpdateAsync(document, async document =>
{
await SetInternalAsync(document);
await InvalidateInternalAsync(document);
if (afterUpdateAsync != null)
{
Expand All @@ -204,7 +204,7 @@ await DocumentStore.UpdateAsync(document, async document =>
// But still update the shared cache after committing.
DocumentStore.AfterCommitSuccess<TDocument>(async () =>
{
await SetInternalAsync(document);
await InvalidateInternalAsync(document);
if (afterUpdateAsync != null)
{
Expand Down Expand Up @@ -352,6 +352,18 @@ protected async Task SetInternalAsync(TDocument document, bool failover = false)
}
}

protected async Task InvalidateInternalAsync(TDocument document, bool failover = false)
{
if (_isDistributed)
{
await _distributedCache.RemoveAsync(_options.CacheIdKey);
}
else
{
_memoryCache.Remove(_options.CacheIdKey);
}
}

private async Task<TDocument> GetFromDistributedCacheAsync()
{
byte[] data = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task UpdateAtomicAsync(Func<Task<TDocument>> updateAsync, Func<TDoc
document.Identifier ??= IdGenerator.GenerateId();
await SetInternalAsync(document);
await InvalidateInternalAsync(document);
if (delegates.AfterUpdateDelegateAsync != null)
{
Expand Down

0 comments on commit c8f1894

Please sign in to comment.