JDA
JDA copied to clipboard
Allow more configuration on data caching
Pull Request Etiquette
- [x] I have checked the PRs for upcoming features/bug fixes.
- [x] I have read the contributing guidelines.
Changes
- [x] Internal code
- [x] Library interface (affecting end-user code)
- [ ] Documentation
- [ ] Other: _____
Closes Issue: NaN
Description
With these changes I introduce XData and MutableXData interfaces which can be used to provide XUpdateDigestEvent rather than atomic events for each changed property. These interfaces can be implemented by the library user and provided using a callback.
new JDABuilder(TOKEN)
.setGuildDataProvider((id) -> new CustomGuildData());
We provide 2 implementations by default:
| Name | Description |
|---|---|
| LIGHT | No data is stored, only default values (constants) |
| RICH | Everything is stored |
You could easily implement custom providers by extending the existing LIGHT configuration:
public class CustomGuildData extends LightGuildData {
private String description = "";
private String bannerId = null;
private String iconId = null;
@Override
public CustomGuildData copy() {
// ...
}
// implement getters and setters
}
Example using rich and having digest events enabled:
@Override
public void onGuildUpdateDigest(@Nonnull GuildUpdateDigestEvent event) {
LOG.info("Received guild update digest for guild {}", event.getGuild().getName());
}
@Override
public void onGenericGuildUpdate(@Nonnull GenericGuildUpdateEvent event) {
if (!(event instanceof GuildUpdateDigestEvent)) {
LOG.info("Received atomic guild update ({}) for guild {}", event.getPropertyIdentifier(), event.getGuild().getName());
}
}
Output:
13:29:01.931 JDA INFO Received guild update digest for guild Testing Bacon
13:29:01.932 JDA INFO Received atomic guild update (max_members) for guild Testing Bacon
13:29:01.933 JDA INFO Received atomic guild update (region) for guild Testing Bacon
13:29:01.933 JDA INFO Received atomic guild update (afk_timeout) for guild Testing Bacon
13:29:01.934 JDA INFO Received atomic guild update (afk_channel) for guild Testing Bacon
13:29:01.934 JDA INFO Received atomic guild update (system_channel) for guild Testing Bacon
I also want to add a flag to switch between either digest events or atomic events. That way we move more towards 1:1 mapping of the events rather than 1:N like we do currently.
This is probably not needed when we allow lazy loading and disabling guild subscriptions.
I want to revisit this for 4.3.0. I also want to make an easy switch to use the LIGHT mode on all entities by default like: builder.setDefaultCachePolicy(CachePolicy.LIGHT) which makes it easier to opt-in on specific entities.