Interface MemberCachePolicy
- All Known Implementing Classes:
LRUMemberCachePolicy
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This will be called throughout JDA when a member gets constructed or modified and allows for a dynamically adjusting cache of users.
When Guild.pruneMemberCache()
is called, the configured policy will be used to unload any members that the policy
has decided not to cache.
If GUILD_MEMBERS
intent is disabled you should not use ALL
or ONLINE
.
This intent enables guild member leave events which are required to remove members from cache properly.
This can be configured with JDABuilder.setMemberCachePolicy(MemberCachePolicy)
.
Example Policy
MemberCachePolicy.VOICE // Keep in cache if currently in voice (skip LRU and ONLINE)
.or(MemberCachePolicy.ONLINE) // Otherwise, only add to cache if online
.and(MemberCachePolicy.lru(1000) // keep 1000 recently active members
.unloadUnless(MemberCachePolicy.VOICE)) // only unload if they are not in voice/guild owner
-
Field Summary
Modifier and TypeFieldDescriptionstatic final MemberCachePolicy
Enable all member caching.static final MemberCachePolicy
Cache members who are boosting the guild.static final MemberCachePolicy
The default policy to use withJDABuilder.createDefault(String)
.static final MemberCachePolicy
Disable all member cachingstatic final MemberCachePolicy
Cache online/idle/dnd users.static final MemberCachePolicy
Cache owner of the guild.static final MemberCachePolicy
Caches members who haven't passed Membership Screening.static final MemberCachePolicy
Cache members who are connected to a voice channel. -
Method Summary
Modifier and TypeMethodDescriptionstatic MemberCachePolicy
all
(MemberCachePolicy policy, MemberCachePolicy... policies) Composes a policy which requires multiple other policies.default MemberCachePolicy
and
(MemberCachePolicy policy) Convenience method to require another policy.static MemberCachePolicy
any
(MemberCachePolicy policy, MemberCachePolicy... policies) Composes a policy by concatenating multiple other policies.boolean
cacheMember
(Member member) Idempotent (ideally pure) function which decided whether to cache the provided member or not.static LRUMemberCachePolicy
lru
(int maxSize) Implementation using a Least-Recently-Used (LRU) cache strategy.default MemberCachePolicy
or
(MemberCachePolicy policy) Convenience method to concatenate another policy.
-
Field Details
-
NONE
Disable all member caching -
ALL
Enable all member caching.Not recommended without
GUILD_MEMBERS
intent enabled. The api will only send the guild member leave events when this intent is enabled. Without those events the members will stay in cache indefinitely. -
OWNER
Cache owner of the guild. This simply checksMember.isOwner()
. -
ONLINE
Cache online/idle/dnd users.
RequiresGatewayIntent.GUILD_PRESENCES
andCacheFlag.ONLINE_STATUS
to be enabled.This cannot cache online members immediately when they come online, due to discord limitations. Discord only sends presence information without member details so the member will be cached once they become active.
Not recommended without
GUILD_MEMBERS
intent enabled. The api will only send the guild member leave events when this intent is enabled. Without those events the members will stay in cache indefinitely. -
VOICE
Cache members who are connected to a voice channel.
RequiresGatewayIntent.GUILD_VOICE_STATES
andCacheFlag.VOICE_STATE
to be enabled. -
BOOSTER
Cache members who are boosting the guild. This checksMember.isBoosting()
RequiresGUILD_MEMBERS
to be enabled. -
PENDING
Caches members who haven't passed Membership Screening.Not recommended without
GUILD_MEMBERS
intent enabled. The api will only send the guild member update events when this intent is enabled. Without those events the members will stay in cache indefinitely.- Incubating:
- Discord is still trying to figure this out
-
DEFAULT
The default policy to use withJDABuilder.createDefault(String)
.
This is identical toVOICE.or(OWNER)
.
-
-
Method Details
-
cacheMember
Idempotent (ideally pure) function which decided whether to cache the provided member or not.
The function should avoid throwing any exceptions or blocking.- Parameters:
member
- The member- Returns:
- True, if the member should be cached
-
or
Convenience method to concatenate another policy.
This is identical to(member) -> policy1.cacheMember(member) || policy2.cacheMember(member)
.- Parameters:
policy
- The policy to concat- Returns:
- New policy which combines both using a logical OR
- Throws:
IllegalArgumentException
- If the provided policy is null
-
and
Convenience method to require another policy.
This is identical to(member) -> policy1.cacheMember(member) && policy2.cacheMember(member)
.- Parameters:
policy
- The policy to require in addition to this one- Returns:
- New policy which combines both using a logical AND
- Throws:
IllegalArgumentException
- If the provided policy is null
-
any
@Nonnull static MemberCachePolicy any(@Nonnull MemberCachePolicy policy, @Nonnull MemberCachePolicy... policies) Composes a policy by concatenating multiple other policies.
This is logically identical topolicy1 || policy2 || policy3 || ... || policyN
.- Parameters:
policy
- The first policypolicies
- The other policies- Returns:
- New policy which combines all provided polices using a logical OR
-
all
@Nonnull static MemberCachePolicy all(@Nonnull MemberCachePolicy policy, @Nonnull MemberCachePolicy... policies) Composes a policy which requires multiple other policies.
This is logically identical topolicy1 && policy2 && policy3 && ... && policyN
.- Parameters:
policy
- The first policypolicies
- The other policies- Returns:
- New policy which combines all provided polices using a logical AND
-
lru
Implementation using a Least-Recently-Used (LRU) cache strategy.Example
This policy would add online members into the pool of cached members. The cached members are limited to 1000 active members, which are handled by the LRU policy. When the LRU cache exceeds the maximum, it will evict the least recently active member from cache. If the sub-policy, in this caseMemberCachePolicy.ONLINE.and( // only cache online members MemberCachePolicy.lru(1000) // of those online members, track the 1000 most active members .unloadUnless(MemberCachePolicy.VOICE) // always keep voice members cached regardless of age )
VOICE
, evaluates totrue
, the member is retained in cache. Otherwise, the member is unloaded usingGuild.unloadMember(long)
.Note that the LRU policy itself always returns
true
forcacheMember(Member)
, since that makes the member the most recently used instead.- Parameters:
maxSize
- The maximum cache capacity of the LRU cache- Returns:
LRUMemberCachePolicy
-