cache

package
v5.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2022 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCache is the default cache.
	DefaultCache Cache = NewCache()
	// DefaultExpiration is the default duration for items stored in
	// the cache to expire.
	DefaultExpiration time.Duration = 0

	// ErrItemExpired is returned in Cache.Get when the item found in the cache
	// has expired.
	ErrItemExpired error = errors.New("item has expired")
	// ErrKeyNotFound is returned in Cache.Get and Cache.Delete when the
	// provided key could not be found in cache.
	ErrKeyNotFound error = errors.New("key not found in cache")
)
View Source
var Plugins = generic.NewContainer(func(opts ...Option) Cache { return nil })

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get gets a cached value by key.
	Get(ctx context.Context, key string) (interface{}, time.Time, error)
	// Put stores a key-value pair into cache.
	Put(ctx context.Context, key string, val interface{}, d time.Duration) error
	// Delete removes a key from cache.
	Delete(ctx context.Context, key string) error
	// String returns the name of the implementation.
	String() string
}

Cache is the interface that wraps the cache.

func NewCache

func NewCache(opts ...Option) Cache

NewCache returns a new cache.

func Provide added in v5.0.2

func Provide(

	_ DiConfig,

	config *Config,
) (Cache, error)

type CacheConfig added in v5.0.2

type CacheConfig struct {
	Enabled bool   `json:"enabled" yaml:"Enabled"`
	Plugin  string `json:"plugin,omitempty" yaml:"Plugin,omitempty"`
}

type Config added in v5.0.2

type Config struct {
	Cache CacheConfig `json:"broker" yaml:"Cache"`
}

func NewConfig added in v5.0.2

func NewConfig() *Config

func (*Config) Merge added in v5.0.2

func (d *Config) Merge(src *Config) error

type DiConfig added in v5.0.2

type DiConfig struct{}

DiConfig is marker that DiFlags has been parsed into Config

func ProvideConfig added in v5.0.2

func ProvideConfig(
	_ di.DiConfig,
	flags *DiFlags,
	config *Config,
	cliConfig *cli.Config,
	configor config.Config,
) (DiConfig, error)

func ProvideConfigNoFlags added in v5.0.2

func ProvideConfigNoFlags(
	config *Config,
	configor config.Config,
) (DiConfig, error)

type DiFlags added in v5.0.2

type DiFlags struct {
	Plugin string
}

func ProvideFlags added in v5.0.2

func ProvideFlags(
	config *Config,
	cliConfig *cli.Config,
	c cli.Cli,
) (*DiFlags, error)

type Item

type Item struct {
	Value      interface{}
	Expiration int64
}

Item represents an item stored in the cache.

func (*Item) Expired

func (i *Item) Expired() bool

Expired returns true if the item has expired.

type Option

type Option func(o *Options)

Option manipulates the Options passed.

func Expiration

func Expiration(d time.Duration) Option

Expiration sets the duration for items stored in the cache to expire.

func Items

func Items(i map[string]Item) Option

Items initializes the cache with preconfigured items.

func WithAddress

func WithAddress(addr string) Option

WithAddress sets the cache service address or connection information.

func WithContext

func WithContext(c context.Context) Option

WithContext sets the cache context, for any extra configuration.

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger sets underline logger.

type Options

type Options struct {
	Expiration time.Duration
	Items      map[string]Item
	// Address represents the address or other connection information of the cache service.
	Address string
	// Context should contain all implementation specific options, using context.WithValue.
	Context context.Context
	// Logger is the be used logger
	Logger logger.Logger
}

Options represents the options for the cache.

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions returns a new options struct.

Jump to

Keyboard shortcuts

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