cache

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Possible cache visibility lookup types.
	VisibilityTypeAccount = VisibilityType('a')
	VisibilityTypeStatus  = VisibilityType('s')
	VisibilityTypeHome    = VisibilityType('h')
	VisibilityTypePublic  = VisibilityType('p')
)

Variables

View Source
var SentinelError = errors.New("BUG: error should not be returned") //nolint:revive

SentinelError is an error that can be returned and checked against to indicate a non-permanent error return from a cache loader callback, e.g. a temporary situation that will soon be fixed.

Functions

This section is empty.

Types

type CachedVisibility added in v0.8.0

type CachedVisibility struct {
	// ItemID is the ID of the item in question (status / account).
	ItemID string

	// RequesterID is the ID of the requesting account for this visibility lookup.
	RequesterID string

	// Type is the visibility lookup type.
	Type VisibilityType

	// Value is the actual visibility value.
	Value bool
}

CachedVisibility represents a cached visibility lookup value.

type Caches added in v0.7.0

type Caches struct {
	// GTS provides access to the collection of
	// gtsmodel object caches. (used by the database).
	GTS GTSCaches

	// AllowHeaderFilters provides access to
	// the allow []headerfilter.Filter cache.
	AllowHeaderFilters headerfilter.Cache

	// BlockHeaderFilters provides access to
	// the block []headerfilter.Filter cache.
	BlockHeaderFilters headerfilter.Cache

	// Visibility provides access to the item visibility
	// cache. (used by the visibility filter).
	Visibility VisibilityCache
	// contains filtered or unexported fields
}

func (*Caches) Init added in v0.7.0

func (c *Caches) Init()

Init will (re)initialize both the GTS and AP cache collections. NOTE: the cache MUST NOT be in use anywhere, this is not thread-safe.

func (*Caches) OnInvalidateAccount added in v0.14.0

func (c *Caches) OnInvalidateAccount(account *gtsmodel.Account)

func (*Caches) OnInvalidateBlock added in v0.14.0

func (c *Caches) OnInvalidateBlock(block *gtsmodel.Block)

func (*Caches) OnInvalidateEmojiCategory added in v0.14.0

func (c *Caches) OnInvalidateEmojiCategory(category *gtsmodel.EmojiCategory)

func (*Caches) OnInvalidateFollow added in v0.14.0

func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow)

func (*Caches) OnInvalidateFollowRequest added in v0.14.0

func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest)

func (*Caches) OnInvalidateList added in v0.14.0

func (c *Caches) OnInvalidateList(list *gtsmodel.List)

func (*Caches) OnInvalidateMedia added in v0.14.0

func (c *Caches) OnInvalidateMedia(media *gtsmodel.MediaAttachment)

func (*Caches) OnInvalidatePoll added in v0.14.0

func (c *Caches) OnInvalidatePoll(poll *gtsmodel.Poll)

func (*Caches) OnInvalidatePollVote added in v0.14.0

func (c *Caches) OnInvalidatePollVote(vote *gtsmodel.PollVote)

func (*Caches) OnInvalidateStatus added in v0.14.0

func (c *Caches) OnInvalidateStatus(status *gtsmodel.Status)

func (*Caches) OnInvalidateStatusFave added in v0.14.0

func (c *Caches) OnInvalidateStatusFave(fave *gtsmodel.StatusFave)

func (*Caches) OnInvalidateUser added in v0.14.0

func (c *Caches) OnInvalidateUser(user *gtsmodel.User)

func (*Caches) Start added in v0.7.0

func (c *Caches) Start()

Start will start any caches that require a background routine, which usually means any kind of TTL caches.

func (*Caches) Stop added in v0.7.0

func (c *Caches) Stop()

Stop will stop any caches that require a background routine, which usually means any kind of TTL caches.

func (*Caches) Sweep added in v0.11.0

func (c *Caches) Sweep(threshold float64)

Sweep will sweep all the available caches to ensure none are above threshold percent full to their total capacity.

This helps with cache performance, as a full cache will require an eviction on every single write, which adds significant overhead to all cache writes.

type GTSCaches added in v0.7.0

type GTSCaches struct {
	// Account provides access to the gtsmodel Account database cache.
	Account StructCache[*gtsmodel.Account]

	// AccountNote provides access to the gtsmodel Note database cache.
	AccountNote StructCache[*gtsmodel.AccountNote]

	// TEMPORARY CACHE TO ALLEVIATE SLOW COUNT QUERIES,
	// (in time will be removed when these IDs are cached).
	AccountCounts *simple.Cache[string, struct {
		Statuses int
		Pinned   int
	}]

	// AccountSettings provides access to the gtsmodel AccountSettings database cache.
	AccountSettings StructCache[*gtsmodel.AccountSettings]

	// Application provides access to the gtsmodel Application database cache.
	Application StructCache[*gtsmodel.Application]

	// Block provides access to the gtsmodel Block (account) database cache.
	Block StructCache[*gtsmodel.Block]

	// FollowIDs provides access to the block IDs database cache.
	BlockIDs SliceCache[string]

	// BoostOfIDs provides access to the boost of IDs list database cache.
	BoostOfIDs SliceCache[string]

	// DomainAllow provides access to the domain allow database cache.
	DomainAllow *domain.Cache

	// DomainBlock provides access to the domain block database cache.
	DomainBlock *domain.Cache

	// Emoji provides access to the gtsmodel Emoji database cache.
	Emoji StructCache[*gtsmodel.Emoji]

	// EmojiCategory provides access to the gtsmodel EmojiCategory database cache.
	EmojiCategory StructCache[*gtsmodel.EmojiCategory]

	// Filter provides access to the gtsmodel Filter database cache.
	Filter StructCache[*gtsmodel.Filter]

	// FilterKeyword provides access to the gtsmodel FilterKeyword database cache.
	FilterKeyword StructCache[*gtsmodel.FilterKeyword]

	// FilterStatus provides access to the gtsmodel FilterStatus database cache.
	FilterStatus StructCache[*gtsmodel.FilterStatus]

	// Follow provides access to the gtsmodel Follow database cache.
	Follow StructCache[*gtsmodel.Follow]

	// FollowIDs provides access to the follower / following IDs database cache.
	// THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
	// - '>'  for following IDs
	// - 'l>' for local following IDs
	// - '<'  for follower IDs
	// - 'l<' for local follower IDs
	FollowIDs SliceCache[string]

	// FollowRequest provides access to the gtsmodel FollowRequest database cache.
	FollowRequest StructCache[*gtsmodel.FollowRequest]

	// FollowRequestIDs provides access to the follow requester / requesting IDs database
	// cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
	// - '>'  for following IDs
	// - '<'  for follower IDs
	FollowRequestIDs SliceCache[string]

	// Instance provides access to the gtsmodel Instance database cache.
	Instance StructCache[*gtsmodel.Instance]

	// InReplyToIDs provides access to the status in reply to IDs list database cache.
	InReplyToIDs SliceCache[string]

	// List provides access to the gtsmodel List database cache.
	List StructCache[*gtsmodel.List]

	// ListEntry provides access to the gtsmodel ListEntry database cache.
	ListEntry StructCache[*gtsmodel.ListEntry]

	// Marker provides access to the gtsmodel Marker database cache.
	Marker StructCache[*gtsmodel.Marker]

	// Media provides access to the gtsmodel Media database cache.
	Media StructCache[*gtsmodel.MediaAttachment]

	// Mention provides access to the gtsmodel Mention database cache.
	Mention StructCache[*gtsmodel.Mention]

	// Move provides access to the gtsmodel Move database cache.
	Move StructCache[*gtsmodel.Move]

	// Notification provides access to the gtsmodel Notification database cache.
	Notification StructCache[*gtsmodel.Notification]

	// Poll provides access to the gtsmodel Poll database cache.
	Poll StructCache[*gtsmodel.Poll]

	// PollVote provides access to the gtsmodel PollVote database cache.
	PollVote StructCache[*gtsmodel.PollVote]

	// PollVoteIDs provides access to the poll vote IDs list database cache.
	PollVoteIDs SliceCache[string]

	// Report provides access to the gtsmodel Report database cache.
	Report StructCache[*gtsmodel.Report]

	// Status provides access to the gtsmodel Status database cache.
	Status StructCache[*gtsmodel.Status]

	// StatusFave provides access to the gtsmodel StatusFave database cache.
	StatusFave StructCache[*gtsmodel.StatusFave]

	// StatusFaveIDs provides access to the status fave IDs list database cache.
	StatusFaveIDs SliceCache[string]

	// Tag provides access to the gtsmodel Tag database cache.
	Tag StructCache[*gtsmodel.Tag]

	// Tombstone provides access to the gtsmodel Tombstone database cache.
	Tombstone StructCache[*gtsmodel.Tombstone]

	// ThreadMute provides access to the gtsmodel ThreadMute database cache.
	ThreadMute StructCache[*gtsmodel.ThreadMute]

	// User provides access to the gtsmodel User database cache.
	User StructCache[*gtsmodel.User]

	// Webfinger provides access to the webfinger URL cache.
	// TODO: move out of GTS caches since unrelated to DB.
	Webfinger *ttl.Cache[string, string] // TTL=24hr, sweep=5min
}

type SliceCache added in v0.11.0

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

SliceCache wraps a simple.Cache to provide simple loader-callback functions for fetching + caching slices of objects (e.g. IDs).

func (*SliceCache[T]) Cap added in v0.15.0

func (c *SliceCache[T]) Cap() int

Cap: see simple.Cache{}.Cap().

func (*SliceCache[T]) Clear added in v0.15.0

func (c *SliceCache[T]) Clear()

Clear: see simple.Cache{}.Clear().

func (*SliceCache[T]) Init added in v0.15.0

func (c *SliceCache[T]) Init(len, cap int)

Init initializes the cache with given length + capacity.

func (*SliceCache[T]) Invalidate added in v0.15.0

func (c *SliceCache[T]) Invalidate(keys ...string)

Invalidate: see simple.Cache{}.InvalidateAll().

func (*SliceCache[T]) Len added in v0.15.0

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

Len: see simple.Cache{}.Len().

func (*SliceCache[T]) Load added in v0.11.0

func (c *SliceCache[T]) Load(key string, load func() ([]T, error)) ([]T, error)

Load will attempt to load an existing slice from cache for key, else calling load function and caching the result.

func (*SliceCache[T]) Trim added in v0.15.0

func (c *SliceCache[T]) Trim(perc float64)

Trim: see simple.Cache{}.Trim().

type StructCache added in v0.15.0

type StructCache[StructType any] struct {
	// contains filtered or unexported fields
}

StructCache wraps a structr.Cache{} to simple index caching by name (also to ease update to library version that introduced this). (in the future it may be worth embedding these indexes by name under the main database caches struct which would reduce time required to access cached values).

func (*StructCache[T]) Cap added in v0.15.0

func (c *StructCache[T]) Cap() int

Cap: see structr.Cache{}.Cap().

func (*StructCache[T]) Clear added in v0.15.0

func (c *StructCache[T]) Clear()

Clear: see structr.Cache{}.Clear().

func (*StructCache[T]) Get added in v0.15.0

func (c *StructCache[T]) Get(index string, keys ...[]any) []T

Get calls structr.Cache{}.Get(), using a cached structr.Index{} by 'index' name. Note: this also handles conversion of the untyped (any) keys to structr.Key{} via structr.Index{}.

func (*StructCache[T]) GetOne added in v0.15.0

func (c *StructCache[T]) GetOne(index string, key ...any) (T, bool)

GetOne calls structr.Cache{}.GetOne(), using a cached structr.Index{} by 'index' name. Note: this also handles conversion of the untyped (any) keys to structr.Key{} via structr.Index{}.

func (*StructCache[T]) Init added in v0.15.0

func (c *StructCache[T]) Init(config structr.CacheConfig[T])

Init initializes the cache with given structr.CacheConfig{}.

func (*StructCache[T]) Invalidate added in v0.15.0

func (c *StructCache[T]) Invalidate(index string, key ...any)

Invalidate calls structr.Cache{}.Invalidate(), using a cached structr.Index{} by 'index' name. Note: this also handles conversion of the untyped (any) keys to structr.Key{} via structr.Index{}.

func (*StructCache[T]) InvalidateIDs added in v0.15.0

func (c *StructCache[T]) InvalidateIDs(index string, ids []string)

InvalidateIDs calls structr.Cache{}.Invalidate(), using a cached structr.Index{} by 'index' name. Note: this also handles conversion of the ID strings to structr.Key{} via structr.Index{}. Strong typing is used for caller convenience.

If you need to invalidate multiple cache keys other than by ID strings, please create another convenience wrapper.

func (*StructCache[T]) Len added in v0.15.0

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

Len: see structr.Cache{}.Len().

func (*StructCache[T]) LoadIDs added in v0.15.0

func (c *StructCache[T]) LoadIDs(index string, ids []string, load func([]string) ([]T, error)) ([]T, error)

LoadIDs calls structr.Cache{}.Load(), using a cached structr.Index{} by 'index' name. Note: this also handles conversion of the ID strings to structr.Key{} via structr.Index{}. Strong typing is used for caller convenience.

If you need to load multiple cache keys other than by ID strings, please create another convenience wrapper.

func (*StructCache[T]) LoadOne added in v0.15.0

func (c *StructCache[T]) LoadOne(index string, load func() (T, error), key ...any) (T, error)

LoadOne calls structr.Cache{}.LoadOne(), using a cached structr.Index{} by 'index' name. Note: this also handles conversion of the untyped (any) keys to structr.Key{} via structr.Index{}.

func (*StructCache[T]) Put added in v0.15.0

func (c *StructCache[T]) Put(values ...T)

Put: see structr.Cache{}.Put().

func (*StructCache[T]) Store added in v0.15.0

func (c *StructCache[T]) Store(value T, store func() error) error

Store: see structr.Cache{}.Store().

func (*StructCache[T]) Trim added in v0.15.0

func (c *StructCache[T]) Trim(perc float64)

Trim: see structr.Cache{}.Trim().

type VisibilityCache added in v0.8.0

type VisibilityCache struct {
	StructCache[*CachedVisibility]
}

type VisibilityType added in v0.8.0

type VisibilityType byte

VisibilityType represents a visibility lookup type. We use a byte type here to improve performance in the result cache when generating the key.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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