cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: LGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Approximate maximum lifetime an attachment can live for without being cleaned up.
	AttachmentLifetime = time.Minute * 5
	// Threshold at which the attachments cache will begin to prune excess elements.
	AttachmentPruneThreshold = 1000
)

Cache cleanup constants.

Variables

View Source
var (
	ErrMissing     = errors.New("cache: entry not present")
	ErrNilProvider = errors.New("cache: attempted to create cache with nil provider")
	ErrIO          = errors.New("cache: attachment download: I/O error")
	ErrRequest     = errors.New("cache: attachment download: network request failed")
	ErrGetFailed   = errors.New("cache: attachment download: http error")
)

Generic errors.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Name, Type    string
	Content       []byte
	LastReference time.Time
}

An Attachment is a generic representation for an attachment downloaded from the Discord API.

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache represents a cache of Discord API data objects.

func NewCache

func NewCache(p Provider) *Cache

NewCache creates a new cache object with provider p.

func (*Cache) Attachment

func (c *Cache) Attachment(at *discordgo.MessageAttachment) (Attachment, error)

Attachment looks up and returns the content and info for a remote attachment from the Discord API. Lookups from the same url are guaranteed not to cause an API hit. Errors are not cached and the attachment is assumed to not exist.

func (*Cache) Channel

func (c *Cache) Channel(ID string) (discordgo.Channel, error)

Channel looks up and returns a channel's data from the discord API, or returns the cached value if already found. If the channel could not be found, error is returned from the discord API. Errors are not cached and failed lookups cause a new API hit.

func (*Cache) Clean

func (c *Cache) Clean()

Clean walks the cache, freeing any bulky cached items which are deemed not particularly useful (e.g attachments which have not been reused in a while).

func (*Cache) Guild

func (c *Cache) Guild(ID string) (discordgo.Guild, error)

Guild looks up and returns a guild's data from the discord API, or returns the cached value if already found. If the guild could not be found, error is returned from the discord API. Errors are not cached and failed lookups cause a new API hit.

func (*Cache) InvalidateChannel

func (c *Cache) InvalidateChannel(ID string) error

InvalidateChannel invalidates the cache entry for a given channel ID.

func (*Cache) InvalidateGuild

func (c *Cache) InvalidateGuild(ID string) error

InvalidateGuild invalidates the cache entry for a given guild ID.

func (*Cache) InvalidateUser

func (c *Cache) InvalidateUser(ID string) error

InvalidateUser invalidates the cache entry for a given user ID.

func (*Cache) User

func (c *Cache) User(ID string) (discordgo.User, error)

User looks up and returns a user's data from the discord API, or returns the cached value if already found. If the user could not be found, error is returned from the discord API. Errors are not cached and failed lookups cause a new API hit.

type Provider

type Provider interface {
	Channel(channelID string) (c *discordgo.Channel, err error)
	User(userID string) (u *discordgo.User, err error)
	Guild(guildID string) (st *discordgo.Guild, err error)
}

Provider is a data provider for discord users and channels. This is mainly for testing and is designed for use with either a mock or *discordgo.Session.

Jump to

Keyboard shortcuts

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