-
Notifications
You must be signed in to change notification settings - Fork 64
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
G11n java client code clean up - remove logic of adding a cacheItem with an empty dataMap to cache for non-supported locales #807
G11n java client code clean up - remove logic of adding a cacheItem with an empty dataMap to cache for non-supported locales #807
Conversation
https://sonarcloud.io/project/issues?id=jessiejuachon-java-client-g11n-java-client&issues=AXDG68NFL24bPpi-pS5s&open=AXDG68NFL24bPpi-pS5s https://sonarcloud.io/project/issues?id=jessiejuachon-java-client-g11n-java-client&issues=AXDG68IGL24bPpi-pS2r&open=AXDG68IGL24bPpi-pS2r Signed-off-by: Jessie <jessiejuachon@gmail.com>
Signed-off-by: Jessie <jessiejuachon@gmail.com>
…and return them upstream.
…t in order to get a 304 NOT MODIFIED http response
…ne if component locale's cache has expired
…ingleton into g11n-java-client
…using cacheExpiredTime config
…g file. This means max age from server will be used. Setting VIPCfg.cacheExpiredTime to 0 disables caching.
…t need to be maintained.
@lyiyu66 , tagging you just for your information. |
This is just the first step (code clean up) in solving 754. Here, I have removed the logic of adding a cacheItem with an empty dataMap to the cache for non-supported locales. We do not need this anymore because we are now first getting the list of supported locales to check if a locale is supported or not. Next step is to use the fallback locale immediately to avoid fetch from service. This will be in another PR. |
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(new LinkedList<>(ps.getSupportedLocales()), Locale.forLanguageTag(dto.getLocale())); | ||
if (ps.isSupportedLocale(matchedLocale)) { // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr") | ||
MessagesDTO matchedLocaleDTO = new MessagesDTO(dto.getComponent(), matchedLocale.toLanguageTag(), dto.getProductID(), dto.getVersion()); | ||
return new ComponentService(matchedLocaleDTO).getMessages(null); |
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.
Seems recursive call is not good.
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.
There is no recursive call in this line. Notice that getMessages is invoked on a separate instance of ComponentService. Could you be more specific as to what you mean by "not good"?
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.
Also, this logic (lines 110-117) is not new. I just moved it out of the createCacheItem method that is called in line 120 into here for code clean up.
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 meant line 115. 'not good' means that it gets the messages twice: one for supported locale, another or not supported locale. The logic is duplicated, the locale should be handled before call getMessages.
Secondly, when i read 'return new ComponentService(matchedLocaleDTO).getMessages(null);', i don't get what it means by passing a null value.
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.
Reminder: This code has been there before this PR. It was for a different issue (#781), so this comment is out of the scope of this PR. Please do not let it block this PR.
The intention of this line is exactly what you mentioned. It is not a duplicate. If requested locale is not found in cache, we want to get a matched locale, if any, instead of the actual requested locale. It is the same strategy for getting messages for a fallback locale: new ComponentService(fallbackLocaleDTO).getMessages()
.
Part of the reason this looks messy is because the locale is currently part of the DTO. This is very old code that I did not want to change especially not in this PR as it is out of scope. In the future, I want to pass the locale to getMessages instead of having it in the DTO. If you do want to track this future improvement, please open another issue, because this PR is for code clean up only, not for changing any logic.
Secondly, passing a null here is unnecessary. I have changed it to getMessages().
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(new LinkedList<>(ps.getSupportedLocales()), Locale.forLanguageTag(dto.getLocale())); | ||
if (ps.isSupportedLocale(matchedLocale)) { // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr") | ||
MessagesDTO matchedLocaleDTO = new MessagesDTO(dto.getComponent(), matchedLocale.toLanguageTag(), dto.getProductID(), dto.getVersion()); | ||
return new ComponentService(matchedLocaleDTO).getMessages(null); |
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.
It's obscure to pass null to getMessages. Hard to understand.
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.
First, this has been there since before this PR, so based on suggestions from retrospective, we should open another issue to address this specific concern. This PR is for code clean up of caching an empty datamap only.
To understand the behavior when passing null to getMessages, you can read the javadoc of getMessages:
* @param fallbackLocalesIter The locale fallback queue to be used on failure. If null, there will be no fallback mechanism on failure so the message map will be empty.
I have logged the issue with description here: #811
} | ||
} | ||
|
||
//Create and store cacheItem for the requested locale | ||
cacheItem = createCacheItem(fallbackLocalesIter); |
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.
Is it necessary to pass fallbackLocalesIter? I think createCacheItem shouldn't be affected by fallbackLocalesIter.
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.
You have already reviewed this code a long time ago though. Yes, it is necessary for now because I don't want to change any code logic in this PR. Please revisit this part in next PR.
if (!ps.isSupportedLocale(Locale.forLanguageTag(dto.getLocale()))) { // Requested locale is not supported | ||
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(new LinkedList<>(ps.getSupportedLocales()), Locale.forLanguageTag(dto.getLocale())); | ||
if (ps.isSupportedLocale(matchedLocale)) { // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr") |
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.
Can't imagine getSupportedLocales is invoked three times in three code lines.
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.
Where? It is only directly invoked in line 112. Do you mean ps.isSupportedLocale? Please clarify your comment (I don't understand what you can't imagine 😄 )
Note that there is formatting logic in ps.isSupportedLocale. For "cleaner" code, I put it in a separate method instead of having to call LocaleUtility.fmtToMappedLocale (which developers may forget to do in the future):
Set<Locale> supportedLocales = getSupportedLocales();
supportedLocales.contains(LocaleUtility.fmtToMappedLocale(...))
Let me know if you have abetter suggestion.
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 think you can improve the LocaleUtility.pickupLocaleFromList to get enough information by only one call. Picking up once to get the supported locale or empty locale.
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.
Ahhh yes! I agree. I already have that lined up. However, as I said, this PR is only to clean up / remove any code related to adding an empty data set into the cache.
Speaking of, I am also removing "locale" property from MessageCacheItem, so more changes have been added to this PR. This is actually related to this code clean up because "locale" was added for the purposes of adding an empty data map into cache for non-supported locales (which we are not doing anymore).
src/main/java/com/vmware/vipclient/i18n/messages/service/ComponentService.java
Outdated
Show resolved
Hide resolved
…nentService.java Co-authored-by: Xiaochao Li <48587632+Xiaochao8@users.noreply.github.com>
Removing locale parameter from MessageCacheItem
@Xiaochao8 , @huihuiw01 , please focus on the intent of the PR. There are already 12 files modified which is quite a lot, so please refrain from discussing changes that are not related to the PR's intent (i.e. clean up code that is related to caching an empty data map for non-supported locales). |
No description provided.