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

Feature/cache support trough redis #430

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Kingmidas74
Copy link
Member

@Kingmidas74 Kingmidas74 commented Mar 26, 2022

Now we can use special extension for caching responses through separate providers (Redis as Example)

@Kingmidas74 Kingmidas74 self-assigned this Mar 27, 2022
@Kingmidas74 Kingmidas74 added this to the 0.13.0 milestone Mar 27, 2022
@Kingmidas74 Kingmidas74 linked an issue Mar 27, 2022 that may be closed by this pull request
@Kingmidas74 Kingmidas74 removed this from the 0.13.0 milestone Mar 27, 2022
@Kingmidas74 Kingmidas74 marked this pull request as ready for review March 27, 2022 14:06
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

79.4% 79.4% Coverage
0.0% 0.0% Duplication

_toolset.Logger?.LogDebug("Response received from cache. Request id: '{requestId}'.", request.Id);
return cachedResult;
}

if (resultType == typeof(TResponse))
return await transportNClient
Copy link
Member

Choose a reason for hiding this comment

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

We need to think about how to cache TResponse

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

.GetHttpResponseAsync(request, resiliencePolicy, cancellationToken)
.ConfigureAwait(false);

await _responseCacheWorker.Put(request, result, cachingAttribute, cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

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

You need to get and save the cache in the transport client. I'll explain why: if you save the cache here, then invalid responses will not be cached, because they will not come here.

{
if (await _responseCacheWorker.TryGet(request, cancellationToken) is { } cachedResult)
Copy link
Member

@smolchanovsky smolchanovsky Mar 27, 2022

Choose a reason for hiding this comment

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

Will the cache work correctly if we have two methods that create the same request, but map the response differently? For example:

[GetMethod("entity")]
public Task<Entity> Get();
[GetMethod("entity")]
public Task<IResult<Entity>> Get();


await _responseCacheWorker.Put(request, result, cachingAttribute, cancellationToken);
return result;
}

if (resultType != typeof(void))
return await transportNClient
Copy link
Member

Choose a reason for hiding this comment

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

You also need to not forget about caching void methods :)

{
if (await _responseCacheWorker.TryGet(request, cancellationToken) is { } cachedResult)
Copy link
Member

Choose a reason for hiding this comment

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

What if the cache becomes unavailable and throws exceptions? I think the client should continue to work without a cache. The same goes for saving the cache

Copy link
Member Author

Choose a reason for hiding this comment

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

Failfast, no?

.GetHttpResponseAsync(request, dataType: resultType.GetGenericArguments().Single(), resiliencePolicy, cancellationToken)
.ConfigureAwait(false);

await _responseCacheWorker.Put(request, result, cachingAttribute, cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

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

I would add logging about saving to the cache too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Built-in Caching opportunity
2 participants