cache

package
v3.7.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: Apache-2.0 Imports: 4 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")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Context(ctx context.Context) Cache
	Get(key string) (interface{}, time.Time, error)
	Put(key string, val interface{}, d time.Duration) error
	Delete(key string) error
}

Cache is the interface that wraps the cache.

Context specifies the context for the cache. Get gets a cached value by key. Put stores a key-value pair into cache. Delete removes a key from cache.

func NewCache

func NewCache(opts ...Option) Cache

NewCache returns a new cache.

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.

type Options

type Options struct {
	Expiration time.Duration
	Items      map[string]Item
}

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