Package net.dv8tion.jda.api.utils.cache
Class LRUMemberCachePolicy
java.lang.Object
net.dv8tion.jda.api.utils.cache.LRUMemberCachePolicy
- All Implemented Interfaces:
MemberCachePolicy
An implementation of a Least-Recently-Used cache.
When the cache capacity exceeds the configured maximum, the eldest cache entry is evicted.
When the cache capacity exceeds the configured maximum, the eldest cache entry is evicted.
You can use unloadUnless(MemberCachePolicy)
, to configure a conditional unloading.
If the configured sub-policy evaluates to true
, the member will not be unloaded even when it is an old cache entry.
This is implemented using a queue and counter algorithm, to achieve amortized O(1) performance.
Example
MemberCachePolicy.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
)
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 case MemberCachePolicy.VOICE
, evaluates to true
, the member is retained in cache.
Otherwise, the member is unloaded using Guild.unloadMember(long)
.
Note that the LRU policy itself always returns true
for cacheMember(Member)
, since that makes the member the most recently used instead.
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionLRUMemberCachePolicy
(int maxMembers) Creates a new instance of the LRU cache policy with the configured maximum capacity. -
Method Summary
Modifier and TypeMethodDescriptionboolean
cacheMember
(Member member) Idempotent (ideally pure) function which decided whether to cache the provided member or not.unloadUnless
(MemberCachePolicy subPolicy) Configure when to unload a member.withActiveMemberCache
(boolean enabled) withActiveMemberCache
(int activityCount) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.dv8tion.jda.api.utils.MemberCachePolicy
and, or
-
Constructor Details
-
LRUMemberCachePolicy
public LRUMemberCachePolicy(int maxMembers) Creates a new instance of the LRU cache policy with the configured maximum capacity.- Parameters:
maxMembers
- The maximum amount members to cache- Throws:
IllegalArgumentException
- If the provided maximum is not positive
-
-
Method Details
-
unloadUnless
Configure when to unload a member.
The provided policy will prevent a member from being uncached, if the policy returns true. This can be useful to have a pool of least-recently-used members cached, while also keeping members required for certain situations in cache.- Parameters:
subPolicy
- The policy to decide when to keep members cached, even when they are old cache entries- Returns:
- The same cache policy instance, with the new sub-policy
- Throws:
IllegalArgumentException
- If the provided policy is null
-
withActiveMemberCache
-
withActiveMemberCache
-
cacheMember
Description copied from interface:MemberCachePolicy
Idempotent (ideally pure) function which decided whether to cache the provided member or not.
The function should avoid throwing any exceptions or blocking.- Specified by:
cacheMember
in interfaceMemberCachePolicy
- Parameters:
member
- The member- Returns:
- True, if the member should be cached
-