cache

package
v0.0.0-...-c18a219 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Constants

View Source
const PkgName = "auxo.cache"

Variables

This section is empty.

Functions

func Exist

func Exist(key string, args ...interface{}) bool

func Get

func Get(key string, args ...interface{}) data.Value

Get returns cached value, the result is assured of not nil.

func Register

func Register(name string, f func(opts data.Map) (Provider, error))

func Remove

func Remove(key string, args ...interface{})

func RemoveGroup

func RemoveGroup(key string)

func Set

func Set(value interface{}, key string, args ...interface{})

Types

type Cacher

type Cacher interface {
	// Get returns cached value, the result is assured of not nil.
	Get(key string, args ...interface{}) data.Value
	Set(value interface{}, key string, args ...interface{})
	Exist(key string, args ...interface{}) bool
	Remove(key string, args ...interface{})
	RemoveGroup(key string)
}

func GetCacher

func GetCacher(name string) (Cacher, error)

type ErrorHandling

type ErrorHandling int

ErrorHandling defines how cache actions behave if the parse fails.

const (
	ErrorSilent ErrorHandling = iota // Do nothing.
	ErrorLog                         // Write log with a descriptive error.
	ErrorPanic                       // Call panic with a descriptive error.
)

These constants cause cache actions to behave as described if the parse fails.

func (*ErrorHandling) Unmarshal

func (e *ErrorHandling) Unmarshal(i interface{}) (err error)

Unmarshal implements config.Unmarshaler interface.

type KeyInfo

type KeyInfo struct {
	Time  time.Duration
	Group string
}

type Keyer

type Keyer func(key string, args ...interface{}) string

type Options

type Options struct {
	Name     string
	Provider string
	Enabled  bool
	Error    ErrorHandling
	Prefix   string
	Time     time.Duration
	Options  data.Map
	Keys     map[string]string
}

type Provider

type Provider interface {
	// Get returns cached value, provider should return data.Nil instead of nil when cache is invalid.
	Get(key string) (data.Value, error)
	Set(key string, value interface{}, expiry time.Duration) error
	Remove(key string) error
	Exist(key string) (bool, error)
}

Provider is cache provider interface.

type Value

type Value[T any] struct {
	TTL  time.Duration
	Load func() (T, error)
	// contains filtered or unexported fields
}

Value is a simple auto refresh cache hold.

func (*Value[T]) Get

func (v *Value[T]) Get(dirty ...bool) (value T, err error)

Get return cached value, it will return expired value if dirty is true and loading failed.

func (*Value[T]) MustGet

func (v *Value[T]) MustGet(dirty ...bool) (value T)

MustGet return cached value, it panics if error occurs.

func (*Value[T]) Reset

func (v *Value[T]) Reset()

Reset clears internal cache value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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