dgocacheler

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 2 Imported by: 0

README

go Go Reference

dgocacheler

A concurrency-safe cache for storing Discord messages by channel, suitable for integration with the discordgo library.

Features

  • Thread-safe operations.
  • Supports adding and retrieving messages.
  • Automatically manages memory by enforcing a maximum cache size per channel.

Installation

go get github.com/CreativeUnicorns/dgocacheler

Usage

Refer to the examples/ directory for example usage.

Contributing

Contributions are welcome! Please feel free to submit a pull request.

License

Distributed under the MIT License. See LICENSE file for more information.

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

Constants

This section is empty.

Variables

View Source
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

func (c *MessageCache) GetMessagesLimit(channelID string, limit int) ([]*discordgo.Message, bool)

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.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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