Class LRUMemberCachePolicy

All Implemented Interfaces:

public class LRUMemberCachePolicy extends Object implements MemberCachePolicy
An implementation of a Least-Recently-Used cache.
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.


 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:
  • Constructor Details

    • LRUMemberCachePolicy

      public LRUMemberCachePolicy(int maxMembers)
      Creates a new instance of the LRU cache policy with the configured maximum capacity.
      maxMembers - The maximum amount members to cache
      IllegalArgumentException - If the provided maximum is not positive
  • Method Details

    • unloadUnless

      @Nonnull public LRUMemberCachePolicy unloadUnless(@Nonnull MemberCachePolicy subPolicy)
      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.
      subPolicy - The policy to decide when to keep members cached, even when they are old cache entries
      The same cache policy instance, with the new sub-policy
      IllegalArgumentException - If the provided policy is null
    • withActiveMemberCache

      @Nonnull public LRUMemberCachePolicy withActiveMemberCache(boolean enabled)
    • withActiveMemberCache

      @Nonnull public LRUMemberCachePolicy withActiveMemberCache(int activityCount)
    • cacheMember

      public boolean cacheMember(@Nonnull Member member)
      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 interface MemberCachePolicy
      member - The member
      True, if the member should be cached