cache

package
v0.11.4 Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: Apache-2.0 Imports: 3 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

func PolicyDefault added in v0.10.3

func PolicyDefault[T any](t T) bool

func PolicyNone added in v0.10.3

func PolicyNone[T any](_ T) bool

Types

type Cache

type Cache[T any] interface {
	Get(id snowflake.ID) (T, bool)
	Put(id snowflake.ID, entity T)
	Remove(id snowflake.ID) (T, bool)
	RemoveIf(filterFunc FilterFunc[T])

	All() []T
	MapAll() map[snowflake.ID]T

	FindFirst(cacheFindFunc FilterFunc[T]) (T, bool)
	FindAll(cacheFindFunc FilterFunc[T]) []T

	ForEach(func(entity T))
}

func NewCache

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

type Caches

type Caches interface {
	CacheFlags() Flags

	GetMemberPermissions(guildID snowflake.ID, member discord.Member) discord.Permissions
	GetMemberPermissionsInChannel(channel discord.GuildChannel, member discord.Member) discord.Permissions
	MemberRoles(member discord.Member) []discord.Role
	AudioChannelMembers(channel discord.GuildAudioChannel) []discord.Member

	GetSelfUser() (discord.OAuth2User, bool)
	PutSelfUser(user discord.OAuth2User)
	GetSelfMember(guildID snowflake.ID) (discord.Member, bool)

	Roles() GroupedCache[discord.Role]
	Members() GroupedCache[discord.Member]
	ThreadMembers() GroupedCache[discord.ThreadMember]
	Presences() GroupedCache[discord.Presence]
	VoiceStates() GroupedCache[discord.VoiceState]
	Messages() GroupedCache[discord.Message]
	Emojis() GroupedCache[discord.Emoji]
	Stickers() GroupedCache[discord.Sticker]
	Guilds() GuildCache
	Channels() ChannelCache
	StageInstances() GroupedCache[discord.StageInstance]
	GuildScheduledEvents() GroupedCache[discord.GuildScheduledEvent]
}

func NewCaches

func NewCaches(opts ...ConfigOpt) Caches

type ChannelCache

type ChannelCache interface {
	Cache[discord.Channel]

	GuildChannels(guildID snowflake.ID) []discord.GuildChannel
	GuildThreadsInChannel(channelID snowflake.ID) []discord.GuildThread

	GetGuildChannel(channelID snowflake.ID) (discord.GuildChannel, bool)
	GetMessageChannel(channelID snowflake.ID) (discord.MessageChannel, bool)
	GetGuildMessageChannel(channelID snowflake.ID) (discord.GuildMessageChannel, bool)
	GetGuildThread(channelID snowflake.ID) (discord.GuildThread, bool)
	GetGuildAudioChannel(channelID snowflake.ID) (discord.GuildAudioChannel, bool)

	GetGuildTextChannel(channelID snowflake.ID) (discord.GuildTextChannel, bool)
	GetDMChannel(channelID snowflake.ID) (discord.DMChannel, bool)
	GetGuildVoiceChannel(channelID snowflake.ID) (discord.GuildVoiceChannel, bool)
	GetGuildCategoryChannel(channelID snowflake.ID) (discord.GuildCategoryChannel, bool)
	GetGuildNewsChannel(channelID snowflake.ID) (discord.GuildNewsChannel, bool)
	GetGuildNewsThread(channelID snowflake.ID) (discord.GuildThread, bool)
	GetGuildPublicThread(channelID snowflake.ID) (discord.GuildThread, bool)
	GetGuildPrivateThread(channelID snowflake.ID) (discord.GuildThread, bool)
	GetGuildStageVoiceChannel(channelID snowflake.ID) (discord.GuildStageVoiceChannel, bool)
}

func NewChannelCache

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

type ChannelCacheImpl

type ChannelCacheImpl struct {
	Cache[discord.Channel]
}

func (*ChannelCacheImpl) GetDMChannel

func (c *ChannelCacheImpl) GetDMChannel(channelID snowflake.ID) (discord.DMChannel, bool)

func (*ChannelCacheImpl) GetGuildAudioChannel

func (c *ChannelCacheImpl) GetGuildAudioChannel(channelID snowflake.ID) (discord.GuildAudioChannel, bool)

func (*ChannelCacheImpl) GetGuildCategoryChannel

func (c *ChannelCacheImpl) GetGuildCategoryChannel(channelID snowflake.ID) (discord.GuildCategoryChannel, bool)

func (*ChannelCacheImpl) GetGuildChannel

func (c *ChannelCacheImpl) GetGuildChannel(channelID snowflake.ID) (discord.GuildChannel, bool)

func (*ChannelCacheImpl) GetGuildMessageChannel

func (c *ChannelCacheImpl) GetGuildMessageChannel(channelID snowflake.ID) (discord.GuildMessageChannel, bool)

func (*ChannelCacheImpl) GetGuildNewsChannel

func (c *ChannelCacheImpl) GetGuildNewsChannel(channelID snowflake.ID) (discord.GuildNewsChannel, bool)

func (*ChannelCacheImpl) GetGuildNewsThread

func (c *ChannelCacheImpl) GetGuildNewsThread(channelID snowflake.ID) (discord.GuildThread, bool)

func (*ChannelCacheImpl) GetGuildPrivateThread

func (c *ChannelCacheImpl) GetGuildPrivateThread(channelID snowflake.ID) (discord.GuildThread, bool)

func (*ChannelCacheImpl) GetGuildPublicThread

func (c *ChannelCacheImpl) GetGuildPublicThread(channelID snowflake.ID) (discord.GuildThread, bool)

func (*ChannelCacheImpl) GetGuildStageVoiceChannel

func (c *ChannelCacheImpl) GetGuildStageVoiceChannel(channelID snowflake.ID) (discord.GuildStageVoiceChannel, bool)

func (*ChannelCacheImpl) GetGuildTextChannel

func (c *ChannelCacheImpl) GetGuildTextChannel(channelID snowflake.ID) (discord.GuildTextChannel, bool)

func (*ChannelCacheImpl) GetGuildThread

func (c *ChannelCacheImpl) GetGuildThread(channelID snowflake.ID) (discord.GuildThread, bool)

func (*ChannelCacheImpl) GetGuildVoiceChannel

func (c *ChannelCacheImpl) GetGuildVoiceChannel(channelID snowflake.ID) (discord.GuildVoiceChannel, bool)

func (*ChannelCacheImpl) GetMessageChannel

func (c *ChannelCacheImpl) GetMessageChannel(channelID snowflake.ID) (discord.MessageChannel, bool)

func (*ChannelCacheImpl) GuildChannels

func (c *ChannelCacheImpl) GuildChannels(guildID snowflake.ID) []discord.GuildChannel

func (*ChannelCacheImpl) GuildThreadsInChannel

func (c *ChannelCacheImpl) GuildThreadsInChannel(channelID snowflake.ID) []discord.GuildThread

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]
}

func DefaultConfig

func DefaultConfig() *Config

func (*Config) Apply

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

type ConfigOpt

type ConfigOpt func(config *Config)

func WithCacheFlags

func WithCacheFlags(flags ...Flags) ConfigOpt

func WithChannelCachePolicy added in v0.10.3

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

func WithEmojiCachePolicy added in v0.10.3

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

func WithGuildCachePolicy added in v0.10.3

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

func WithGuildScheduledEventCachePolicy added in v0.10.3

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

func WithMemberCachePolicy

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

func WithMessageCachePolicy

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

func WithPresenceCachePolicy added in v0.10.3

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

func WithRoleCachePolicy added in v0.10.3

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

func WithStageInstanceCachePolicy added in v0.10.3

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

func WithStickerCachePolicy added in v0.10.3

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

func WithThreadMemberCachePolicy added in v0.10.3

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

func WithVoiceStateCachePolicy added in v0.10.3

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

type DefaultCache

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

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]) 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 DefaultGroupedCache

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

func (*DefaultGroupedCache[T]) All

func (c *DefaultGroupedCache[T]) All() map[snowflake.ID][]T

func (*DefaultGroupedCache[T]) FindAll

func (c *DefaultGroupedCache[T]) FindAll(cacheFindFunc GroupedCacheFilterFunc[T]) []T

func (*DefaultGroupedCache[T]) FindFirst

func (c *DefaultGroupedCache[T]) FindFirst(cacheFindFunc GroupedCacheFilterFunc[T]) (T, bool)

func (*DefaultGroupedCache[T]) ForEach

func (c *DefaultGroupedCache[T]) ForEach(forEachFunc func(groupID snowflake.ID, entity T))

func (*DefaultGroupedCache[T]) ForEachGroup

func (c *DefaultGroupedCache[T]) ForEachGroup(groupID snowflake.ID, forEachFunc func(entity T))

func (*DefaultGroupedCache[T]) Get

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

func (*DefaultGroupedCache[T]) GroupAll

func (c *DefaultGroupedCache[T]) GroupAll(groupID snowflake.ID) []T

func (*DefaultGroupedCache[T]) GroupFindAll

func (c *DefaultGroupedCache[T]) GroupFindAll(groupID snowflake.ID, cacheFindFunc GroupedCacheFilterFunc[T]) []T

func (*DefaultGroupedCache[T]) GroupFindFirst

func (c *DefaultGroupedCache[T]) GroupFindFirst(groupID snowflake.ID, cacheFindFunc GroupedCacheFilterFunc[T]) (T, bool)

func (*DefaultGroupedCache[T]) MapAll

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

func (*DefaultGroupedCache[T]) MapGroupAll

func (c *DefaultGroupedCache[T]) MapGroupAll(groupID snowflake.ID) map[snowflake.ID]T

func (*DefaultGroupedCache[T]) Put

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

func (*DefaultGroupedCache[T]) Remove

func (c *DefaultGroupedCache[T]) Remove(groupID snowflake.ID, id snowflake.ID) (entity T, ok bool)

func (*DefaultGroupedCache[T]) RemoveAll

func (c *DefaultGroupedCache[T]) RemoveAll(groupID snowflake.ID)

func (*DefaultGroupedCache[T]) RemoveIf

func (c *DefaultGroupedCache[T]) RemoveIf(filterFunc GroupedCacheFilterFunc[T])

type FilterFunc

type FilterFunc[T any] func(T) bool

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(groupID snowflake.ID, id snowflake.ID) (T, bool)
	Put(groupID snowflake.ID, id snowflake.ID, entity T)
	Remove(groupID snowflake.ID, id snowflake.ID) (T, bool)
	RemoveAll(groupID snowflake.ID)
	RemoveIf(filterFunc GroupedCacheFilterFunc[T])

	All() map[snowflake.ID][]T
	GroupAll(groupID snowflake.ID) []T

	MapAll() map[snowflake.ID]map[snowflake.ID]T
	MapGroupAll(groupID snowflake.ID) map[snowflake.ID]T

	FindFirst(cacheFindFunc GroupedCacheFilterFunc[T]) (T, bool)
	GroupFindFirst(groupID snowflake.ID, cacheFindFunc GroupedCacheFilterFunc[T]) (T, bool)

	FindAll(cacheFindFunc GroupedCacheFilterFunc[T]) []T
	GroupFindAll(groupID snowflake.ID, cacheFindFunc GroupedCacheFilterFunc[T]) []T

	ForEach(func(groupID snowflake.ID, entity T))
	ForEachGroup(groupID snowflake.ID, forEachFunc func(entity T))
}

func NewGroupedCache

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

type GroupedCacheFilterFunc

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

type GuildCache

type GuildCache interface {
	Cache[discord.Guild]

	SetReady(shardID int, guildID snowflake.ID)
	SetUnready(shardID int, guildID snowflake.ID)
	IsUnready(shardID int, guildID snowflake.ID) bool
	UnreadyGuilds(shardID int) []snowflake.ID

	SetUnavailable(guildID snowflake.ID)
	SetAvailable(guildID snowflake.ID)
	IsUnavailable(guildID snowflake.ID) bool
	UnavailableGuilds() []snowflake.ID
}

func NewGuildCache

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

type GuildCacheImpl

type GuildCacheImpl struct {
	Cache[discord.Guild]
	// contains filtered or unexported fields
}

func (*GuildCacheImpl) IsUnavailable

func (c *GuildCacheImpl) IsUnavailable(guildID snowflake.ID) bool

func (*GuildCacheImpl) IsUnready

func (c *GuildCacheImpl) IsUnready(shardID int, guildID snowflake.ID) bool

func (*GuildCacheImpl) SetAvailable

func (c *GuildCacheImpl) SetAvailable(guildID snowflake.ID)

func (*GuildCacheImpl) SetReady

func (c *GuildCacheImpl) SetReady(shardID int, guildID snowflake.ID)

func (*GuildCacheImpl) SetUnavailable

func (c *GuildCacheImpl) SetUnavailable(id snowflake.ID)

func (*GuildCacheImpl) SetUnready

func (c *GuildCacheImpl) SetUnready(shardID int, guildID snowflake.ID)

func (*GuildCacheImpl) UnavailableGuilds

func (c *GuildCacheImpl) UnavailableGuilds() []snowflake.ID

func (*GuildCacheImpl) UnreadyGuilds

func (c *GuildCacheImpl) UnreadyGuilds(shardID int) []snowflake.ID

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 (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