JDA icon indicating copy to clipboard operation
JDA copied to clipboard

Allow more configuration on data caching

Open MinnDevelopment opened this issue 6 years ago • 3 comments

Pull Request Etiquette

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

MinnDevelopment avatar Jul 02 '19 11:07 MinnDevelopment

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.

MinnDevelopment avatar Jul 02 '19 12:07 MinnDevelopment

This is probably not needed when we allow lazy loading and disabling guild subscriptions.

MinnDevelopment avatar Aug 06 '19 12:08 MinnDevelopment

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.

MinnDevelopment avatar Dec 26 '19 08:12 MinnDevelopment