cache

package
v0.13.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2022 License: Apache-2.0 Imports: 4 Imported by: 35

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PolicyAll added in v0.10.3

func PolicyAll[T any](_ T) bool

PolicyAll returns a policy that will cache all entities.

func PolicyDefault added in v0.10.3

func PolicyDefault[T any](t T) bool

PolicyDefault returns the default cache policy.

func PolicyMembersPending added in v0.12.0

func PolicyMembersPending(member discord.Member) bool

PolicyMembersPending is a policy that will only cache members that are pending.

func PolicyNone added in v0.10.3

func PolicyNone[T any](_ T) bool

PolicyNone returns a policy that will never cache anything.

Types

type Cache

type Cache[T any] interface {
	// Get returns a copy of the entity with the given snowflake and a bool weather it was found or not.
	Get(id snowflake.ID) (T, bool)

	// Put stores the given entity with the given snowflake as key. If the entity is already present, it will be overwritten.
	Put(id snowflake.ID, entity T)

	// Remove removes the entity with the given snowflake as key and returns a copy of the entity and a bool whether it was removed or not.
	Remove(id snowflake.ID) (T, bool)

	// RemoveIf removes all entities that pass the given FilterFunc
	RemoveIf(filterFunc FilterFunc[T])

	// Len returns the number of entities in the cache.
	Len() int

	// All returns a copy of all entities in this cache as a slice.
	All() []T

	// MapAll returns a copy of all entities in this cache as a map.
	MapAll() map[snowflake.ID]T

	// FindFirst returns the first entity that passes the given FilterFunc and a bool whether it was found or not.
	FindFirst(cacheFindFunc FilterFunc[T]) (T, bool)

	// FindAll returns all entities that pass the given FilterFunc as a slice.
	FindAll(cacheFindFunc FilterFunc[T]) []T

	// ForEach calls the given function for each entity in the cache.
	ForEach(func(entity T))
}

Cache is a simple key value store. They key is always a snowflake.ID. The cache provides a simple way to store and retrieve entities. But is not guaranteed to be thread safe as this depends on the underlying implementation.

func NewCache

func NewCache[T any](flags Flags, neededFlags Flags, policy Policy[T]) Cache[T]

NewCache returns a new DefaultCache implementation which filter the entities after the gives Flags and Policy. This cache implementation is thread safe and can be used in multiple goroutines without any issues. It also only hands out copies to the entities. Regardless these entities should be handles as immutable.

type Caches

type Caches interface {
	// CacheFlags returns the current configured FLags of the caches.
	CacheFlags() Flags

	// GetMemberPermissions returns the calculated permissions of the given member.
	// This requires the FlagRoles to be set.
	GetMemberPermissions(member discord.Member) discord.Permissions

	// GetMemberPermissionsInChannel returns the calculated permissions of the given member in the given channel.
	// This requires the FlagRoles and FlagChannels to be set.
	GetMemberPermissionsInChannel(channel discord.GuildChannel, member discord.Member) discord.Permissions

	// MemberRoles returns all roles of the given member.
	// This requires the FlagRoles to be set.
	MemberRoles(member discord.Member) []discord.Role

	// AudioChannelMembers returns all members which are in the given audio channel.
	// This requires the FlagVoiceStates to be set.
	AudioChannelMembers(channel discord.GuildAudioChannel) []discord.Member

	// GetSelfUser returns the current bot user.
	// This is only available after we received the gateway.EventTypeReady event.
	GetSelfUser() (discord.OAuth2User, bool)

	// PutSelfUser overrides the current bot user.
	PutSelfUser(user discord.OAuth2User)

	// GetSelfMember returns the current bot member from the given guildID.
	// This is only available after we received the gateway.EventTypeGuildCreate event for the given guildID.
	GetSelfMember(guildID snowflake.ID) (discord.Member, bool)

	// Roles returns the role cache.
	Roles() GroupedCache[discord.Role]

	// Members returns the member cache.
	Members() GroupedCache[discord.Member]

	// ThreadMembers returns the thread member cache.
	ThreadMembers() GroupedCache[discord.ThreadMember]

	// Presences returns the presence cache.
	Presences() GroupedCache[discord.Presence]

	// VoiceStates returns the voice state cache.
	VoiceStates() GroupedCache[discord.VoiceState]

	// Messages returns the message cache.
	Messages() GroupedCache[discord.Message]

	// Emojis returns the emoji cache.
	Emojis() GroupedCache[discord.Emoji]

	// Stickers returns the sticker cache.
	Stickers() GroupedCache[discord.Sticker]

	// Guilds returns the guild cache.
	Guilds() GuildCache

	// Channels returns the channel cache.
	Channels() ChannelCache

	// StageInstances returns the stage instance cache.
	StageInstances() GroupedCache[discord.StageInstance]

	// GuildScheduledEvents returns the guild scheduled event cache.
	GuildScheduledEvents() GroupedCache[discord.GuildScheduledEvent]
}

Caches combines all different entity caches into one with some utility methods.

func New added in v0.12.0

func New(opts ...ConfigOpt) Caches

New returns a new default Caches instance with the given ConfigOpt(s) applied.

type ChannelCache

type ChannelCache interface {
	Cache[discord.Channel]

	// GuildChannels returns all discord.GuildChannel in a guild and a bool indicating if it exists.
	GuildChannels(guildID snowflake.ID) []discord.GuildChannel

	// GuildThreadsInChannel returns all discord.GuildThread from the ChannelCache and a bool indicating if it exists.
	GuildThreadsInChannel(channelID snowflake.ID) []discord.GuildThread

	// GetGuildChannel returns a discord.GuildChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildChannel(channelID snowflake.ID) (discord.GuildChannel, bool)

	// GetMessageChannel returns a discord.MessageChannel from the ChannelCache and a bool indicating if it exists.
	GetMessageChannel(channelID snowflake.ID) (discord.MessageChannel, bool)

	// GetGuildMessageChannel returns a discord.GuildMessageChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildMessageChannel(channelID snowflake.ID) (discord.GuildMessageChannel, bool)

	// GetGuildThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists.
	GetGuildThread(channelID snowflake.ID) (discord.GuildThread, bool)

	// GetGuildAudioChannel returns a discord.GetGuildAudioChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildAudioChannel(channelID snowflake.ID) (discord.GuildAudioChannel, bool)

	// GetGuildTextChannel returns a discord.GuildTextChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildTextChannel(channelID snowflake.ID) (discord.GuildTextChannel, bool)

	// GetDMChannel returns a discord.DMChannel from the ChannelCache and a bool indicating if it exists.
	GetDMChannel(channelID snowflake.ID) (discord.DMChannel, bool)

	// GetGuildVoiceChannel returns a discord.GuildVoiceChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildVoiceChannel(channelID snowflake.ID) (discord.GuildVoiceChannel, bool)

	// GetGuildCategoryChannel returns a discord.GuildCategoryChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildCategoryChannel(channelID snowflake.ID) (discord.GuildCategoryChannel, bool)

	// GetGuildNewsChannel returns a discord.GuildNewsChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildNewsChannel(channelID snowflake.ID) (discord.GuildNewsChannel, bool)

	// GetGuildNewsThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists.
	GetGuildNewsThread(channelID snowflake.ID) (discord.GuildThread, bool)

	// GetGuildPublicThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists.
	GetGuildPublicThread(channelID snowflake.ID) (discord.GuildThread, bool)

	// GetGuildPrivateThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists.
	GetGuildPrivateThread(channelID snowflake.ID) (discord.GuildThread, bool)

	// GetGuildStageVoiceChannel returns a discord.GuildStageVoiceChannel from the ChannelCache and a bool indicating if it exists.
	GetGuildStageVoiceChannel(channelID snowflake.ID) (discord.GuildStageVoiceChannel, bool)
}

ChannelCache is a Cache for all channel types

func NewChannelCache

func NewChannelCache(flags Flags, policy Policy[discord.Channel]) ChannelCache

NewChannelCache returns a new channelCacheImpl with the given flags and policy. channelCacheImpl is thread safe and can be used in multiple goroutines.

type Config

type Config struct {
	CacheFlags Flags

	GuildCachePolicy               Policy[discord.Guild]
	ChannelCachePolicy             Policy[discord.Channel]
	StageInstanceCachePolicy       Policy[discord.StageInstance]
	GuildScheduledEventCachePolicy Policy[discord.GuildScheduledEvent]
	RoleCachePolicy                Policy[discord.Role]
	MemberCachePolicy              Policy[discord.Member]
	ThreadMemberCachePolicy        Policy[discord.ThreadMember]
	PresenceCachePolicy            Policy[discord.Presence]
	VoiceStateCachePolicy          Policy[discord.VoiceState]
	MessageCachePolicy             Policy[discord.Message]
	EmojiCachePolicy               Policy[discord.Emoji]
	StickerCachePolicy             Policy[discord.Sticker]
}

Config lets you configure your Caches instance.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults.

func (*Config) Apply

func (c *Config) Apply(opts []ConfigOpt)

Apply applies the given ConfigOpt(s) to the Config

type ConfigOpt

type ConfigOpt func(config *Config)

ConfigOpt is a type alias for a function that takes a Config and is used to configure your Caches.

func WithCacheFlags

func WithCacheFlags(flags ...Flags) ConfigOpt

WithCacheFlags sets the Flags of the Config.

func WithChannelCachePolicy added in v0.10.3

func WithChannelCachePolicy(policy Policy[discord.Channel]) ConfigOpt

WithChannelCachePolicy sets the Policy[discord.Channel] of the Config.

func WithEmojiCachePolicy added in v0.10.3

func WithEmojiCachePolicy(policy Policy[discord.Emoji]) ConfigOpt

WithEmojiCachePolicy sets the Policy[discord.Emoji] of the Config.

func WithGuildCachePolicy added in v0.10.3

func WithGuildCachePolicy(policy Policy[discord.Guild]) ConfigOpt

WithGuildCachePolicy sets the Policy[discord.Guild] of the Config.

func WithGuildScheduledEventCachePolicy added in v0.10.3

func WithGuildScheduledEventCachePolicy(policy Policy[discord.GuildScheduledEvent]) ConfigOpt

WithGuildScheduledEventCachePolicy sets the Policy[discord.GuildScheduledEvent] of the Config.

func WithMemberCachePolicy

func WithMemberCachePolicy(policy Policy[discord.Member]) ConfigOpt

WithMemberCachePolicy sets the Policy[discord.Member] of the Config.

func WithMessageCachePolicy

func WithMessageCachePolicy(policy Policy[discord.Message]) ConfigOpt

WithMessageCachePolicy sets the Policy[discord.Message] of the Config.

func WithPresenceCachePolicy added in v0.10.3

func WithPresenceCachePolicy(policy Policy[discord.Presence]) ConfigOpt

WithPresenceCachePolicy sets the Policy[discord.Presence] of the Config.

func WithRoleCachePolicy added in v0.10.3

func WithRoleCachePolicy(policy Policy[discord.Role]) ConfigOpt

WithRoleCachePolicy sets the Policy[discord.Role] of the Config.

func WithStageInstanceCachePolicy added in v0.10.3

func WithStageInstanceCachePolicy(policy Policy[discord.StageInstance]) ConfigOpt

WithStageInstanceCachePolicy sets the Policy[discord.Guild] of the Config.

func WithStickerCachePolicy added in v0.10.3

func WithStickerCachePolicy(policy Policy[discord.Sticker]) ConfigOpt

WithStickerCachePolicy sets the Policy[discord.Sticker] of the Config.

func WithThreadMemberCachePolicy added in v0.10.3

func WithThreadMemberCachePolicy(policy Policy[discord.ThreadMember]) ConfigOpt

WithThreadMemberCachePolicy sets the Policy[discord.ThreadMember] of the Config.

func WithVoiceStateCachePolicy added in v0.10.3

func WithVoiceStateCachePolicy(policy Policy[discord.VoiceState]) ConfigOpt

WithVoiceStateCachePolicy sets the Policy[discord.VoiceState] of the Config.

type DefaultCache

type DefaultCache[T any] struct {
	// contains filtered or unexported fields
}

DefaultCache is a simple thread safe cache key value store.

func (*DefaultCache[T]) All

func (c *DefaultCache[T]) All() []T

func (*DefaultCache[T]) FindAll

func (c *DefaultCache[T]) FindAll(cacheFindFunc FilterFunc[T]) []T

func (*DefaultCache[T]) FindFirst

func (c *DefaultCache[T]) FindFirst(cacheFindFunc FilterFunc[T]) (T, bool)

func (*DefaultCache[T]) ForEach

func (c *DefaultCache[T]) ForEach(forEachFunc func(entity T))

func (*DefaultCache[T]) Get

func (c *DefaultCache[T]) Get(id snowflake.ID) (T, bool)

func (*DefaultCache[T]) Len added in v0.12.0

func (c *DefaultCache[T]) Len() int

func (*DefaultCache[T]) MapAll

func (c *DefaultCache[T]) MapAll() map[snowflake.ID]T

func (*DefaultCache[T]) Put

func (c *DefaultCache[T]) Put(id snowflake.ID, entity T)

func (*DefaultCache[T]) Remove

func (c *DefaultCache[T]) Remove(id snowflake.ID) (T, bool)

func (*DefaultCache[T]) RemoveIf

func (c *DefaultCache[T]) RemoveIf(filterFunc FilterFunc[T])

type FilterFunc

type FilterFunc[T any] func(T) bool

FilterFunc is used to filter cached entities.

type Flags

type Flags int

Flags are used to enable/disable certain internal caches

const (
	FlagGuilds Flags = 1 << iota
	FlagGuildScheduledEvents
	FlagMembers
	FlagThreadMembers
	FlagMessages
	FlagPresences
	FlagChannels
	FlagRoles
	FlagEmojis
	FlagStickers
	FlagVoiceStates
	FlagStageInstances
	FlagsNone Flags = 0

	FlagsDefault = FlagsNone

	FlagsAll = FlagGuilds |
		FlagGuildScheduledEvents |
		FlagChannels |
		FlagRoles |
		FlagEmojis |
		FlagStickers |
		FlagVoiceStates |
		FlagStageInstances |
		FlagPresences
)

values for CacheFlags

func (Flags) Add

func (f Flags) Add(bits ...Flags) Flags

Add allows you to add multiple bits together, producing a new bit

func (Flags) Has

func (f Flags) Has(bits ...Flags) bool

Has will ensure that the bit includes all the bits entered

func (Flags) Missing

func (f Flags) Missing(bits ...Flags) bool

Missing will check whether the bit is missing any one of the bits

func (Flags) Remove

func (f Flags) Remove(bits ...Flags) Flags

Remove allows you to subtract multiple bits from the first, producing a new bit

type GroupedCache

type GroupedCache[T any] interface {
	// Get returns a copy of the entity with the given groupID and ID and a bool wheaten it was found or not.
	Get(groupID snowflake.ID, id snowflake.ID) (T, bool)

	// Put stores the given entity with the given groupID and ID as key. If the entity is already present, it will be overwritten.
	Put(groupID snowflake.ID, id snowflake.ID, entity T)

	// Remove removes the entity with the given groupID and ID as key and returns a copy of the entity and a bool whether it was removed or not.
	Remove(groupID snowflake.ID, id snowflake.ID) (T, bool)

	// RemoveAll removes all entities in the given groupID.
	RemoveAll(groupID snowflake.ID)

	// RemoveIf removes all entities that pass the given GroupedFilterFunc
	RemoveIf(filterFunc GroupedFilterFunc[T])

	// Len returns the total number of entities in the cache.
	Len() int

	// GroupLen returns the number of entities in the cache within the groupID.
	GroupLen(groupID snowflake.ID) int

	// All returns a copy of all entities in the cache.
	All() map[snowflake.ID][]T

	// GroupAll returns a copy of all entities in a specific group.
	GroupAll(groupID snowflake.ID) []T

	// MapAll returns a copy of all entities in the cache as a map.
	MapAll() map[snowflake.ID]map[snowflake.ID]T

	// MapGroupAll returns a copy of all entities in a specific group as a map.
	MapGroupAll(groupID snowflake.ID) map[snowflake.ID]T

	// FindFirst returns the first entity that passes the given GroupedFilterFunc.
	FindFirst(cacheFindFunc GroupedFilterFunc[T]) (T, bool)

	// GroupFindFirst returns the first entity that passes the given GroupedFilterFunc within the groupID.
	GroupFindFirst(groupID snowflake.ID, cacheFindFunc GroupedFilterFunc[T]) (T, bool)

	// FindAll returns all entities that pass the given GroupedFilterFunc.
	FindAll(cacheFindFunc GroupedFilterFunc[T]) []T

	// GroupFindAll returns all entities that pass the given GroupedFilterFunc within the groupID.
	GroupFindAll(groupID snowflake.ID, cacheFindFunc GroupedFilterFunc[T]) []T

	// ForEach calls the given function for each entity in the cache.
	ForEach(func(groupID snowflake.ID, entity T))

	// GroupForEach calls the given function for each entity in the cache within the groupID.
	GroupForEach(groupID snowflake.ID, forEachFunc func(entity T))
}

GroupedCache is a simple key value store grouped by a snowflake.ID. They key is always a snowflake.ID. The cache provides a simple way to store and retrieve entities. But is not guaranteed to be thread safe as this depends on the underlying implementation.

func NewGroupedCache

func NewGroupedCache[T any](flags Flags, neededFlags Flags, policy Policy[T]) GroupedCache[T]

NewGroupedCache returns a new default GroupedCache with the provided flags, neededFlags and policy.

type GroupedFilterFunc added in v0.12.0

type GroupedFilterFunc[T any] func(groupID snowflake.ID, entity T) bool

GroupedFilterFunc is used to filter grouped cached entities.

type GuildCache

type GuildCache interface {
	Cache[discord.Guild]

	// SetReady sets the specified guildID as ready in the specific shard.
	SetReady(shardID int, guildID snowflake.ID)

	// SetUnready sets the specified guildID as not ready in the specific shard.
	SetUnready(shardID int, guildID snowflake.ID)

	// IsUnready returns a bool indicating if the specified guildID is ready or not in the specific shard.
	IsUnready(shardID int, guildID snowflake.ID) bool

	// UnreadyGuilds returns all guildIDs that are not ready in the specific shard.
	UnreadyGuilds(shardID int) []snowflake.ID

	// SetUnavailable sets the specified guildID as unavailable.
	SetUnavailable(guildID snowflake.ID)

	// SetAvailable sets the specified guildID as available.
	SetAvailable(guildID snowflake.ID)

	// IsUnavailable returns a bool indicating if the specified guildID is unavailable or not.
	IsUnavailable(guildID snowflake.ID) bool

	// UnavailableGuilds returns all guildIDs that are unavailable.
	UnavailableGuilds() []snowflake.ID
}

GuildCache is a Cache for guilds. It also keeps track of unready and unavailable guilds.

func NewGuildCache

func NewGuildCache(flags Flags, policy Policy[discord.Guild]) GuildCache

NewGuildCache a new guildCacheImpl with the given flags and policy. guildCacheImpl is thread safe and can be used in multiple goroutines.

type Policy

type Policy[T any] func(entity T) bool

Policy can be used to define your own policy for when entities should be cached.

func AllPolicies

func AllPolicies[T any](policies ...Policy[T]) Policy[T]

AllPolicies is a shorthand for CachePolicy.And(CachePolicy).And(CachePolicy) etc.

func AnyPolicy

func AnyPolicy[T any](policies ...Policy[T]) Policy[T]

AnyPolicy is a shorthand for CachePolicy.Or(CachePolicy).Or(CachePolicy) etc.

func PolicyChannelExclude added in v0.12.0

func PolicyChannelExclude(channelTypes ...discord.ChannelType) Policy[discord.Channel]

PolicyChannelExclude returns a policy that will not cache channels of the given types.

func PolicyChannelInclude added in v0.12.0

func PolicyChannelInclude(channelTypes ...discord.ChannelType) Policy[discord.Channel]

PolicyChannelInclude returns a policy that will only cache channels of the given types.

func PolicyMembersInVoice added in v0.12.0

func PolicyMembersInVoice(caches Caches) Policy[discord.Member]

PolicyMembersInVoice returns a policy that will only cache members that are connected to an audio channel.

func PolicyMembersInclude added in v0.12.0

func PolicyMembersInclude(guildIDs ...snowflake.ID) Policy[discord.Member]

PolicyMembersInclude returns a policy that will only cache members of the given guilds.

func (Policy[T]) And

func (p Policy[T]) And(policy Policy[T]) Policy[T]

And allows you to require both CachePolicy(s) to be true for the entity to be cached

func (Policy[T]) Or

func (p Policy[T]) Or(policy Policy[T]) Policy[T]

Or allows you to combine the CachePolicy with another, meaning either of them needs to be true

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL