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

Remove unnecessary deserialization actions #14904

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -364,23 +364,20 @@ protected async Task InvalidateInternalAsync(TDocument document, bool failover =

private async Task<TDocument> GetFromDistributedCacheAsync()
{
byte[] data = null;

if (_isDistributed)
{
data = await _distributedCache.GetAsync(_options.CacheKey);
byte[] data = await _distributedCache.GetAsync(_options.CacheKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use var data

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the suggestion feature instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
byte[] data = await _distributedCache.GetAsync(_options.CacheKey);
var data = await _distributedCache.GetAsync(_options.CacheKey);

if (data == null)
{
return null;
}
return await _options.Serializer.DeserializeAsync<TDocument>(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty line after the ending } brace

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return await _options.Serializer.DeserializeAsync<TDocument>(data);
return await _options.Serializer.DeserializeAsync<TDocument>(data);

}
else if (_memoryCache.TryGetValue<TDocument>(_options.CacheKey, out var cached))
{
data = await _options.Serializer.SerializeAsync(cached);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact there is a reason for doing this, this only happens if not distributed and on updating, when we want to get a mutable document for updating for which we don't want to return a shared document from the memory cache.

So serializing and deserializing is a way to clone the document.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your reply. I didn't think of these, maybe we should add some notes to the text here

}

if (data == null)
{
return null;
return cached;
}

return await _options.Serializer.DeserializeAsync<TDocument>(data);
return null;
}

private async Task UpdateDistributedCacheAsync(TDocument document)
Expand Down