-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Feat access token cache #1249
base: develop
Are you sure you want to change the base?
Feat access token cache #1249
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's Good
Hi Mohsen! What issue is this PR related to? |
@EngRajabi commented on May 30, 2020:
In comparison to what performance indicators was the current performance improved? |
I was in an enterprise project that used the reference token type. I had a performance problem. Because in every request, it needs to request the identity of the server. The problem was solved with this cache. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge conflict
Did you use Service Discovery + Identity Server setup, or classic setup + Identity Server Bearer Tokens? |
Unfortunately there are no builds for these commits! |
cd5675c
to
0561ad5
Compare
Mohsen, congrats! 🎉 Now we can go with code review... And, once again, |
_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json"; | ||
_cacheKey = "InternalDiskFileConfigurationRepository"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be a constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with Ray!
It should be public static const
field or property.
What is the strange key value?
Can we write "BlaBlaBlaKey"? 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_cacheKey = "InternalDiskFileConfigurationRepository"; | |
_cacheKey = nameof(DiskFileConfigurationRepository); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have found
- 1 major issue
- a few minor issues aka hardcode
- minor styling & formatting issues
Could you fix them, Mohsen, please?
@@ -83,7 +83,7 @@ public async Task<Response> Set(FileConfiguration ocelotConfiguration) | |||
|
|||
if (result.Response) | |||
{ | |||
_cache.AddAndDelete(_configurationKey, ocelotConfiguration, TimeSpan.FromSeconds(3), _configurationKey); | |||
_cache.AddAndDelete(_configurationKey, ocelotConfiguration, TimeSpan.FromSeconds(5), _configurationKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${\color{orange}Hardcode\ has\ been\ detected!}$
It is better to define public static const
field.
Is this value used somewhere else?
As I understood, we cache for 5 seconds, right?
Why 3 or 5 seconds?
public AuthenticationMiddleware(RequestDelegate next, | ||
IOcelotLoggerFactory loggerFactory) | ||
IOcelotLoggerFactory loggerFactory, IMemoryCache memoryCache) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Styling issue
public AuthenticationMiddleware(RequestDelegate next, | |
IOcelotLoggerFactory loggerFactory) | |
IOcelotLoggerFactory loggerFactory, IMemoryCache memoryCache) | |
public AuthenticationMiddleware( | |
RequestDelegate next, | |
IOcelotLoggerFactory loggerFactory, | |
IMemoryCache memoryCache) |
Goal: Each param is located on own line.
Or, put these params into one line. It's OK because there are up to 3 params only.
Logger.LogInformation( | ||
$"{httpContext.Request.Path} is an authenticated route. {MiddlewareName} checking if client is authenticated"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting issue: Should be one line!
var result = | ||
await httpContext.AuthenticateAsync(downstreamRoute.AuthenticationOptions | ||
.AuthenticationProviderKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting issue: 3 lines!
But it should be one line actually.
|
||
userClaim = result.Principal; | ||
|
||
_memoryCache.Set(cacheKey, userClaim, TimeSpan.FromMinutes(5)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${\color{orange}Hardcode\ has\ been\ detected!}$
5
is hardcode.
Should be public static const
field.
_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json"; | ||
_cacheKey = "InternalDiskFileConfigurationRepository"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with Ray!
It should be public static const
field or property.
What is the strange key value?
Can we write "BlaBlaBlaKey"? 🤣
_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json"; | ||
_cacheKey = "InternalDiskFileConfigurationRepository"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_cacheKey = "InternalDiskFileConfigurationRepository"; | |
_cacheKey = nameof(DiskFileConfigurationRepository); |
@@ -63,6 +76,9 @@ public Task<Response> Set(FileConfiguration fileConfiguration) | |||
} | |||
|
|||
_changeTokenSource.Activate(); | |||
|
|||
_cache.AddAndDelete(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${\color{orange}Hardcode\ has\ been\ detected!}$
5
should be public static const
field or property.
Why do you use _cacheKey
as a value of the region
parameter?
{ | ||
return Task.FromResult<Response<FileConfiguration>>(new OkResponse<FileConfiguration>(configuration)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${\color{red}No\ adding\ the\ value\ to\ the\ cache!}$
Current implementation reads the value from the cache, and returns the value if it exists.
When the cache is empty, after reading the file content in lock
and deserialization in line 52, we have to add the value to the cache immediately.
So, you forgot to renew the cache by Add
method after the line 52:
lock (_lock)
{
jsonConfiguration = System.IO.File.ReadAllText(_environmentFilePath);
}
var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(jsonConfiguration);
// TODO Add this line
_cache.AddAndDelete(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey);
// OR
_cache.Add(_cacheKey, fileConfiguration, TimeSpan.FromMinutes(5), _cacheKey);
// return
Questions 👇Based on PR title "Feat access token cache", I would say that I cannot get: how do you manage caching of access token? Maybe you were inspired by some issue from backlog? |
0561ad5
to
c8c0634
Compare
c8c0634
to
4c2b93e
Compare
New Feature
Cache response access token
Motivation for New Feature