Skip to content

Commit

Permalink
Improve usage of cache in Guild#retrieveMemberById (#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Aug 3, 2022
1 parent 28db46a commit b579a8d
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/main/java/net/dv8tion/jda/internal/entities/GuildImpl.java
Expand Up @@ -16,6 +16,9 @@

package net.dv8tion.jda.internal.entities;

import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import gnu.trove.set.TLongSet;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.Region;
import net.dv8tion.jda.api.audio.hooks.ConnectionStatus;
Expand Down Expand Up @@ -73,7 +76,12 @@
import net.dv8tion.jda.internal.utils.cache.SnowflakeCacheViewImpl;
import net.dv8tion.jda.internal.utils.cache.SortedSnowflakeCacheViewImpl;
import net.dv8tion.jda.internal.utils.concurrent.task.GatewayTask;
import okhttp3.MediaType;
import okhttp3.MultipartBody;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.OffsetDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.*;
Expand All @@ -83,16 +91,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import gnu.trove.set.TLongSet;
import okhttp3.MediaType;
import okhttp3.MultipartBody;

public class GuildImpl implements Guild
{
private final long id;
Expand Down Expand Up @@ -1098,28 +1096,14 @@ public Task<Void> loadMembers(@Nonnull Consumer<Member> callback)
return new GatewayTask<>(handler, () -> handler.cancel(false));
}

// Helper function for deferred cache access
private Member getMember(long id, JDAImpl jda)
{
if (jda.isIntent(GatewayIntent.GUILD_MEMBERS))
{
// return member from cache if member tracking is enabled through intents
Member member = getMemberById(id);
// if the join time is inaccurate we also have to load it through REST to update this information
if (member != null && member.hasTimeJoined())
return member;
}
return null;
}

@Nonnull
@Override
public CacheRestAction<Member> retrieveMemberById(long id)
{
JDAImpl jda = getJDA();
return new DeferredRestAction<>(jda, Member.class,
() -> getMember(id, jda),
() -> { // otherwise we need to update the member with a REST request first to get the nickname/roles
() -> getMemberById(id),
() -> {
if (id == jda.getSelfUser().getIdLong())
return new CompletedRestAction<>(jda, getSelfMember());
Route.CompiledRoute route = Route.Guilds.GET_MEMBER.compile(getId(), Long.toUnsignedString(id));
Expand All @@ -1128,7 +1112,7 @@ public CacheRestAction<Member> retrieveMemberById(long id)
jda.getEntityBuilder().updateMemberCache(member);
return member;
});
});
}).useCache(jda.isIntent(GatewayIntent.GUILD_MEMBERS));
}

@Nonnull
Expand Down

0 comments on commit b579a8d

Please sign in to comment.