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

Conversation

hyzx86
Copy link
Contributor

@hyzx86 hyzx86 commented Dec 15, 2023

When distributed caching is not enabled, redundant serialization and deserialization actions are performed

@hyzx86 hyzx86 requested a review from jtkech as a code owner December 15, 2023 09:27
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

{
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

}
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 (_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.

You can use the suggestion feature instead

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.

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

{
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.

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

@hyzx86 hyzx86 closed this Dec 15, 2023
@hyzx86 hyzx86 deleted the patch-16 branch June 13, 2024 06:15
@hyzx86 hyzx86 restored the patch-16 branch June 13, 2024 06:23
@hyzx86 hyzx86 deleted the patch-16 branch June 13, 2024 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants