Documentation ¶
Overview ¶
Package cacheler provides a concurrency-safe cache designed specifically for storing Discord messages by channel. It integrates smoothly with the discordgo package.
The dgocacheler package ensures that all operations are safe to use concurrently and manages memory efficiently by enforcing a maximum number of messages per channel.
The package is designed to be simple to use and easy to integrate with existing chatbot handlers code. The dgocacheler package also provides a global `Cache` that can be used across multiple packages to help avoid circular dependencies.
Index ¶
- Variables
- type MessageCache
- func (c *MessageCache) AddMessage(channelID string, message *discordgo.Message)
- func (c *MessageCache) AddMessages(channelID string, messages []*discordgo.Message)
- func (c *MessageCache) GetMessages(channelID string) ([]*discordgo.Message, bool)
- func (c *MessageCache) GetMessagesLimit(channelID string, limit int) ([]*discordgo.Message, bool)
- func (c *MessageCache) SetMaxMessages(maxMessages int)
Constants ¶
This section is empty.
Variables ¶
var Cache = NewMessageCache(100)
Global cache
Functions ¶
This section is empty.
Types ¶
type MessageCache ¶
type MessageCache struct { sync.RWMutex // Embedding RWMutex to provide locking // contains filtered or unexported fields }
MessageCache holds Discord messages organized by channel ID. It supports concurrent access.
func NewMessageCache ¶
func NewMessageCache(maxMessages int) *MessageCache
NewMessageCache creates a new MessageCache with a specified maximum number of messages per channel.
func (*MessageCache) AddMessage ¶
func (c *MessageCache) AddMessage(channelID string, message *discordgo.Message)
AddMessage adds a single message to the cache for a specific channel.
func (*MessageCache) AddMessages ¶
func (c *MessageCache) AddMessages(channelID string, messages []*discordgo.Message)
AddMessages adds multiple messages to the cache for a specific channel.
func (*MessageCache) GetMessages ¶
func (c *MessageCache) GetMessages(channelID string) ([]*discordgo.Message, bool)
GetMessages retrieves all messages for a given channel from the cache
func (*MessageCache) GetMessagesLimit ¶
GetMessagesLimit retrieves up to a specified number of recent messages for a given channel.
func (*MessageCache) SetMaxMessages ¶
func (c *MessageCache) SetMaxMessages(maxMessages int)
SetMaxMessages sets the maximum number of messages to store per channel in the cache.