Class MemoryEntityCache

java.lang.Object
com.mewna.catnip.cache.MemoryEntityCache
All Implemented Interfaces:
EntityCache, EntityCacheWorker
Direct Known Subclasses:
SplitMemoryEntityCache, UnifiedMemoryEntityCache

public abstract class MemoryEntityCache extends Object implements EntityCacheWorker
Since:
9/18/18.
Author:
amy
  • Field Details

  • Constructor Details

    • MemoryEntityCache

      public MemoryEntityCache()
  • Method Details

    • canProvidePreviousState

      public boolean canProvidePreviousState(@Nonnull EntityCacheWorker.CachedEntityState state)
      Specified by:
      canProvidePreviousState in interface EntityCacheWorker
    • memberNameFunction

      @Nonnull @CheckReturnValue protected Function<Member,String> memberNameFunction()
      Function used to map members to their name, for named cache views. Used by the default createMemberCacheView() and members() implementations.

      Defaults to returning a member's effective name, which is their nickname, if present, or their username.

      Returns:
      Function used to map members to their name.
    • createGuildCacheView

      @Nonnull @CheckReturnValue protected MutableNamedCacheView<Guild> createGuildCacheView()
      Creates a new guild cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new guild cache view.
    • createUserCacheView

      @Nonnull @CheckReturnValue protected MutableNamedCacheView<User> createUserCacheView()
      Creates a new user cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new user cache view.
    • createDMChannelCacheView

      @Nonnull @CheckReturnValue protected MutableCacheView<UserDMChannel> createDMChannelCacheView()
      Creates a new DM channel cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new DM channel cache view.
    • createPresenceCacheView

      @Nonnull @CheckReturnValue protected MutableCacheView<Presence> createPresenceCacheView()
      Creates a new presence cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new presence cache view.
    • createGuildChannelCacheView

      @Nonnull @CheckReturnValue protected MutableNamedCacheView<GuildChannel> createGuildChannelCacheView()
      Creates a new guild channel cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new guild channel cache view.
    • createRoleCacheView

      @Nonnull @CheckReturnValue protected MutableNamedCacheView<Role> createRoleCacheView()
      Creates a new role cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new role cache view.
    • createMemberCacheView

      @Nonnull @CheckReturnValue protected MutableNamedCacheView<Member> createMemberCacheView()
      Creates a new member cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new member cache view.
    • createEmojiCacheView

      @Nonnull @CheckReturnValue protected MutableNamedCacheView<Emoji.CustomEmoji> createEmojiCacheView()
      Creates a new emoji cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new emoji cache view.
    • createVoiceStateCacheView

      @Nonnull @CheckReturnValue protected MutableCacheView<VoiceState> createVoiceStateCacheView()
      Creates a new voice state cache view. Subclasses can override this method to use a different cache view implementation.
      Returns:
      A new voice state cache view.
    • createCacheView

      @Nonnull @CheckReturnValue protected <T> MutableCacheView<T> createCacheView()
      Creates a new cache view. Subclasses can override this method to use a different cache view implementation.
      Type Parameters:
      T - Type of the elements to be held by this view.
      Returns:
      A new cache view.
    • createNamedCacheView

      @Nonnull @CheckReturnValue protected <T> MutableNamedCacheView<T> createNamedCacheView(@Nonnull Function<T,String> nameFunction)
      Creates a new named cache view. Subclasses can override this method to use a different cache view implementation.
      Type Parameters:
      T - Type of the elements to be held by this view.
      Returns:
      A new named cache view.
    • userCache

      protected abstract MutableNamedCacheView<User> userCache(int shardId)
    • dmChannelCache

      protected abstract MutableCacheView<UserDMChannel> dmChannelCache(int shardId)
    • presenceCache

      protected abstract MutableCacheView<Presence> presenceCache(int shardId)
    • guildCache

      protected MutableNamedCacheView<Guild> guildCache(int shardId)
    • memberCache

      protected MutableNamedCacheView<Member> memberCache(long guildId, boolean onlyGet)
    • deleteMemberCache

      protected void deleteMemberCache(long guildId)
    • roleCache

      protected MutableNamedCacheView<Role> roleCache(long guildId, boolean onlyGet)
    • deleteRoleCache

      protected void deleteRoleCache(long guildId)
    • channelCache

      protected MutableNamedCacheView<GuildChannel> channelCache(long guildId, boolean onlyGet)
    • deleteChannelCache

      protected void deleteChannelCache(long guildId)
    • emojiCache

      protected MutableNamedCacheView<Emoji.CustomEmoji> emojiCache(long guildId, boolean onlyGet)
    • deleteEmojiCache

      protected void deleteEmojiCache(long guildId)
    • voiceStateCache

      protected MutableCacheView<VoiceState> voiceStateCache(long guildId, boolean onlyGet)
    • deleteVoiceStateCache

      protected void deleteVoiceStateCache(long guildId)
    • deleteThreadCache

      protected void deleteThreadCache(long guildId)
    • or

      protected <T> io.reactivex.rxjava3.core.Maybe<T> or(@Nullable T data, T def)
    • or

      protected <T> io.reactivex.rxjava3.core.Maybe<T> or(@Nullable T data)
    • guild

      @Nonnull public io.reactivex.rxjava3.core.Maybe<Guild> guild(long id)
      Description copied from interface: EntityCache
      Get the guild with the specified ID. May be null.
      Specified by:
      guild in interface EntityCache
      Parameters:
      id - The ID of the guild to fetch.
      Returns:
      The guild, or null if it isn't cached.
    • user

      @Nonnull public io.reactivex.rxjava3.core.Maybe<User> user(long id)
      Description copied from interface: EntityCache
      Get the user with the specified ID. May be null.
      Specified by:
      user in interface EntityCache
      Parameters:
      id - The ID of the user to fetch.
      Returns:
      The user, or null if it isn't cached.
    • presence

      @Nonnull public io.reactivex.rxjava3.core.Maybe<Presence> presence(long id)
      Description copied from interface: EntityCache
      Get the presence for the user with the specified ID. May be null.
      Specified by:
      presence in interface EntityCache
      Parameters:
      id - The ID of the user whose presence is to be fetched.
      Returns:
      The user's presence, or null if it isn't cached.
    • member

      @Nonnull public io.reactivex.rxjava3.core.Maybe<Member> member(long guildId, long id)
      Description copied from interface: EntityCache
      Get the member with the given ID from the guild with the given ID. May be null.
      Specified by:
      member in interface EntityCache
      Parameters:
      guildId - The ID of the guild the desired member is in.
      id - The ID of the desired member.
      Returns:
      The member, or null if it isn't cached.
    • role

      @Nonnull public io.reactivex.rxjava3.core.Maybe<Role> role(long guildId, long id)
      Description copied from interface: EntityCache
      Get the role with the given ID from the guild with the given ID. May be null.
      Specified by:
      role in interface EntityCache
      Parameters:
      guildId - The ID of the guild the desired role is from.
      id - The ID of the desired role.
      Returns:
      The role, or null if it isn't cached.
    • channel

      @Nonnull public io.reactivex.rxjava3.core.Maybe<GuildChannel> channel(long guildId, long id)
      Description copied from interface: EntityCache
      Get the channel with the given ID from the guild with the given ID. May be null.
      Specified by:
      channel in interface EntityCache
      Parameters:
      guildId - The ID of the guild the desired channel is from.
      id - The ID of the desired channel.
      Returns:
      The channel, or null if it isn't cached.
    • emoji

      @Nonnull public io.reactivex.rxjava3.core.Maybe<Emoji.CustomEmoji> emoji(long guildId, long id)
      Description copied from interface: EntityCache
      Get the custom emojis with the given ID from the guild with the given ID. May be null,
      Specified by:
      emoji in interface EntityCache
      Parameters:
      guildId - The ID of the guild the desired custom emojis is from.
      id - The ID of the desired custom emojis.
      Returns:
      The custom emojis, or null if it isn't cached.
    • voiceState

      @Nonnull public io.reactivex.rxjava3.core.Maybe<VoiceState> voiceState(long guildId, long id)
      Description copied from interface: EntityCache
      Get the voice state for the user with the given ID, possibly in the guild with the given ID. May be null.
      Specified by:
      voiceState in interface EntityCache
      Parameters:
      guildId - The ID of the guild the voice state is from.
      id - The ID of the user whose voice state is desired.
      Returns:
      The requested voice state, or null if it isn't cached.
    • threadMember

      @Nonnull public io.reactivex.rxjava3.core.Maybe<ThreadChannel.ThreadMember> threadMember(long id)
      Specified by:
      threadMember in interface EntityCache
    • selfUser

      @Nonnull public io.reactivex.rxjava3.core.Maybe<User> selfUser()
      Specified by:
      selfUser in interface EntityCache
      Returns:
      The currently-logged-in user. May be null if no shards have logged in.
    • shardId

      protected int shardId(long entityId)
    • updateCache

      @Nonnull public io.reactivex.rxjava3.core.Completable updateCache(@Nonnull String eventType, @Nonnegative int shardId, @Nonnull com.grack.nanojson.JsonObject payload)
      Description copied from interface: EntityCacheWorker
      Update cache with a single gateway event.
      Specified by:
      updateCache in interface EntityCacheWorker
      Parameters:
      eventType - Type of the event.
      payload - Data payload contained in the event
      Returns:
      Itself.
    • bulkCacheUsers

      public void bulkCacheUsers(@Nonnegative int shardId, @Nonnull Collection<User> users)
      Specified by:
      bulkCacheUsers in interface EntityCacheWorker
    • bulkCacheChannels

      public void bulkCacheChannels(@Nonnegative int shardId, @Nonnull Collection<GuildChannel> channels)
      Specified by:
      bulkCacheChannels in interface EntityCacheWorker
    • bulkCacheRoles

      public void bulkCacheRoles(@Nonnegative int shardId, @Nonnull Collection<Role> roles)
      Specified by:
      bulkCacheRoles in interface EntityCacheWorker
    • bulkCacheMembers

      public void bulkCacheMembers(@Nonnegative int shardId, @Nonnull Collection<Member> members)
      Specified by:
      bulkCacheMembers in interface EntityCacheWorker
    • bulkCacheEmoji

      public void bulkCacheEmoji(@Nonnegative int shardId, @Nonnull Collection<Emoji.CustomEmoji> emoji)
      Specified by:
      bulkCacheEmoji in interface EntityCacheWorker
    • bulkCachePresences

      public void bulkCachePresences(@Nonnegative int shardId, @Nonnull Map<String,Presence> presences)
      Specified by:
      bulkCachePresences in interface EntityCacheWorker
    • bulkCacheVoiceStates

      public void bulkCacheVoiceStates(@Nonnegative int shardId, @Nonnull Collection<VoiceState> voiceStates)
      Specified by:
      bulkCacheVoiceStates in interface EntityCacheWorker
    • bulkCacheThreadMembers

      public void bulkCacheThreadMembers(int shardId, @Nonnull Collection<ThreadChannel.ThreadMember> threadMembers)
      Specified by:
      bulkCacheThreadMembers in interface EntityCacheWorker
    • invalidateShard

      public void invalidateShard(int id)
      Specified by:
      invalidateShard in interface EntityCacheWorker
    • guilds

      @Nonnull public NamedCacheView<Guild> guilds()
      Specified by:
      guilds in interface EntityCache
      Returns:
      A view of the current guild cache. Updates to the cache will update this view.
    • members

      @Nonnull public NamedCacheView<Member> members(long guildId)
      Description copied from interface: EntityCache
      Get all members for the guild with the given ID.
      Specified by:
      members in interface EntityCache
      Parameters:
      guildId - The ID of the guild to fetch members for.
      Returns:
      A view of the current member cache of the guild. Updates to the cache will update this view.
    • members

      @Nonnull public NamedCacheView<Member> members()
      Description copied from interface: EntityCache
      Get all members cached in this entity cache.
      Specified by:
      members in interface EntityCache
      Returns:
      A view of all the current member caches. Updates to the caches or additions/removals (of guilds) will update this view.
    • roles

      @Nonnull public NamedCacheView<Role> roles(long guildId)
      Description copied from interface: EntityCache
      Get all roles for the guild with the given ID.
      Specified by:
      roles in interface EntityCache
      Parameters:
      guildId - The ID of the guild to fetch roles for.
      Returns:
      A view of the current role cache of the guild. Updates to the cache will update this view.
    • roles

      @Nonnull public NamedCacheView<Role> roles()
      Description copied from interface: EntityCache
      Get all roles cached in this entity cache.
      Specified by:
      roles in interface EntityCache
      Returns:
      A view of all the current role caches. Updates to the caches or additions/removals (of guilds) will update this view.
    • channels

      @Nonnull public NamedCacheView<GuildChannel> channels(long guildId)
      Description copied from interface: EntityCache
      Get all channels for the guild with the given ID.
      Specified by:
      channels in interface EntityCache
      Parameters:
      guildId - The ID of the guild to fetch channels for.
      Returns:
      A view of the current channel cache of the guild. Updates to the cache will update this view.
    • channels

      @Nonnull public NamedCacheView<GuildChannel> channels()
      Description copied from interface: EntityCache
      Get all guild channels cached in this entity cache.
      Specified by:
      channels in interface EntityCache
      Returns:
      A view of all the current guild channel caches. Updates to the caches or additions/removals (of guilds) will update this view.
    • emojis

      @Nonnull public NamedCacheView<Emoji.CustomEmoji> emojis(long guildId)
      Description copied from interface: EntityCache
      Get all custom emojis for the guild with the given ID.
      Specified by:
      emojis in interface EntityCache
      Parameters:
      guildId - The ID of the guild to fetch custom emojis for.
      Returns:
      A view of the current emoji cache of the guild. Updates to the cache will update this view.
    • emojis

      @Nonnull public NamedCacheView<Emoji.CustomEmoji> emojis()
      Description copied from interface: EntityCache
      Get all emojis cached in this entity cache.
      Specified by:
      emojis in interface EntityCache
      Returns:
      A view of all the current emoji caches. Updates to the caches or additions/removals (of guilds) will update this view.
    • threadMembers

      @Nonnull public CacheView<ThreadChannel.ThreadMember> threadMembers()
      Specified by:
      threadMembers in interface EntityCache
    • voiceStates

      @Nonnull public CacheView<VoiceState> voiceStates(long guildId)
      Description copied from interface: EntityCache
      Get all voice states for the guild with the given ID.
      Specified by:
      voiceStates in interface EntityCache
      Parameters:
      guildId - The ID of the guild to fetch voice states for.
      Returns:
      A view of the current voice state cache of the guild. Updates to the cache will update this view.
    • voiceStates

      @Nonnull public CacheView<VoiceState> voiceStates()
      Description copied from interface: EntityCache
      Get all voice states for the entire bot.
      Specified by:
      voiceStates in interface EntityCache
      Returns:
      A view of all the current voice state caches. Updates to the caches or additions/removals (of guilds) will update this view.
    • catnip

      @Nonnull public EntityCache catnip(@Nonnull Catnip catnip)
      Specified by:
      catnip in interface EntityCacheWorker
    • catnip

      public Catnip catnip()