-
Notifications
You must be signed in to change notification settings - Fork 7
Updates cache with response #147
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
Closed
NightTripperID
wants to merge
15
commits into
javadiscord:main
from
NightTripperID:update-cache-with-response
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9b06559
enhancement: adds caching to DiscordResponseParser
NightTripperID 8bebf2d
enhancement: adds CacheHandler
NightTripperID 9d9d537
chore: updates naming
NightTripperID 63cf503
chore: updates comment
NightTripperID 4eb979b
Merge branch 'main' of https://github.com/NightTripperID/java-discord…
NightTripperID bae9b84
chore: adds null check to CacheUpdater
NightTripperID 6517867
chore: fixes field fetching in CacheUpdater
NightTripperID 700b1fa
chore: fixes formatting (spotlessApply)
NightTripperID b94f6ab
chore: adds null check to CacheUpdater
NightTripperID f432fbf
chore: adds tracing logs
NightTripperID e33edf0
chore: adds cache updating to parseFromMap
NightTripperID 924be7b
chore: fixes exception message formatting
NightTripperID 8b02a37
chore: fixes exception message formatting
NightTripperID 7f43674
chore: updates trace loggging
NightTripperID 22c5430
chore: updates exception handling
NightTripperID File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
api/src/main/java/com/javadiscord/jdi/core/api/utils/CacheUpdater.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.javadiscord.jdi.core.api.utils; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
import com.javadiscord.jdi.internal.cache.Cache; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class CacheUpdater { | ||
|
||
private final Cache cache; | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(CacheUpdater.class); | ||
|
||
public CacheUpdater(Cache cache) { | ||
this.cache = cache; | ||
} | ||
|
||
public <T> void updateCache(T result) { | ||
if (result == null) { | ||
return; | ||
} | ||
try { | ||
Field guildIdField = result.getClass().getDeclaredField("guildId"); | ||
Field idField = result.getClass().getDeclaredField("id"); | ||
|
||
long guildId = getLongFromField(guildIdField, result); | ||
long id = getLongFromField(idField, result); | ||
|
||
if (cache.getCacheForGuild(guildId) == null) { | ||
LOGGER.trace( | ||
"Failed to cache result of type {} with guildId of {}", | ||
result.getClass().getName(), guildId | ||
); | ||
} else { | ||
cache.getCacheForGuild(guildId).add(id, result); | ||
} | ||
|
||
} catch (IllegalAccessException | NoSuchFieldException | NumberFormatException e) { | ||
LOGGER.trace( | ||
"Failed to cache result of type {}, cause: {}", | ||
result.getClass().getName(), e.getMessage() | ||
); | ||
} | ||
} | ||
|
||
public <T> void updateCache(List<T> resultList) { | ||
resultList.forEach(this::updateCache); | ||
} | ||
|
||
private <T> long getLongFromField( | ||
Field field, | ||
T result | ||
) throws IllegalAccessException, NumberFormatException { | ||
field.setAccessible(true); | ||
if (field.getType() == String.class) { | ||
return Long.parseLong((String) field.get(result)); | ||
} | ||
return (long) field.get(result); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.