item

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExceededRejectRetryLimit = errors.New("item: exceeded lease rejected retry limit")

ErrExceededRejectRetryLimit returned when number of rejected lease gets exceed number of sleep durations

View Source
var ErrInvalidLeaseGetStatus = errors.New("item: invalid lease get response status")

ErrInvalidLeaseGetStatus ...

View Source
var ErrNotFound = errors.New("item: not found")

ErrNotFound ONLY be returned from the filler function, to do delete of lease get key in the memcached server

Functions

func DefaultSleepDurations

func DefaultSleepDurations() []time.Duration

DefaultSleepDurations ...

Types

type Filler

type Filler[T any, K any] func(ctx context.Context, key K) func() (T, error)

Filler is for getting data from the backing store and set back to memcached servers

func NewMultiGetFiller

func NewMultiGetFiller[T any, K comparable](
	multiGetFunc func(ctx context.Context, keys []K) ([]T, error),
	getKey func(v T) K,
	options ...MultiGetFillerOption,
) Filler[T, K]

NewMultiGetFiller ...

type GetState added in v1.1.0

type GetState[T Value, K Key] struct {
	// contains filtered or unexported fields
}

GetState store intermediate state when getting item

func (*GetState[T, K]) Result added in v1.1.0

func (s *GetState[T, K]) Result() (T, error)

Result returns result

type Item

type Item[T Value, K Key] struct {
	// contains filtered or unexported fields
}

Item is NOT thread safe and, it contains a cached keys once a key is cached in memory, it will return the same value unless call **Reset**

func New

func New[T Value, K Key](
	pipeline memproxy.Pipeline,
	unmarshaler Unmarshaler[T],
	filler Filler[T, K],
	options ...Option,
) *Item[T, K]

New creates an item.Item. Param: unmarshaler is for unmarshalling the Value type. Param: filler is for fetching data from the backing source (e.g. Database), and can use the function NewMultiGetFiller() for simple multi get from database

func (*Item[T, K]) Get

func (i *Item[T, K]) Get(ctx context.Context, key K) func() (T, error)

Get a single item with key

func (*Item[T, K]) GetFast added in v1.1.0

func (i *Item[T, K]) GetFast(ctx context.Context, key K) *GetState[T, K]

GetFast is similar to Get but reduced one alloc

func (*Item[T, K]) GetMulti added in v1.2.0

func (i *Item[T, K]) GetMulti(ctx context.Context, keys []K) func() ([]T, error)

GetMulti gets multiple keys at once

func (*Item[T, K]) GetStats

func (i *Item[T, K]) GetStats() Stats

GetStats ...

func (*Item[T, K]) LowerSession

func (i *Item[T, K]) LowerSession() memproxy.Session

LowerSession ...

func (*Item[T, K]) Reset

func (i *Item[T, K]) Reset()

Reset clear in-memory cached values

type Key

type Key interface {
	comparable
	String() string
}

Key is the key constraint

type MultiGetFillerOption

type MultiGetFillerOption func(conf *multiGetFillerConfig)

MultiGetFillerOption ...

func WithMultiGetEnableDeleteOnNotFound

func WithMultiGetEnableDeleteOnNotFound(enable bool) MultiGetFillerOption

WithMultiGetEnableDeleteOnNotFound when enable = true will delete the empty key-value (used for lease get) from memcached server, when the multiGetFunc NOT returning the corresponding values for the keys. Otherwise, the empty value (zero value) will be set to the memcached server. By default, enable = false.

type Option

type Option func(opts *itemOptions)

Option ...

func WithEnableErrorOnExceedRetryLimit

func WithEnableErrorOnExceedRetryLimit(enable bool) Option

WithEnableErrorOnExceedRetryLimit enables returning error if sleepDurations exceeded when enable = true, and after retried all the durations configured by WithSleepDurations, the Item.Get will return the error ErrExceededRejectRetryLimit default enable = false

func WithEnableFillingOnCacheError

func WithEnableFillingOnCacheError(enable bool) Option

WithEnableFillingOnCacheError when enable = true, continue to read from DB when get from memcached returns error default enable = false

func WithErrorLogger

func WithErrorLogger(logger func(err error)) Option

WithErrorLogger configures the error logger when there are problems with the memcache client or unmarshalling

func WithSleepDurations

func WithSleepDurations(durations ...time.Duration) Option

WithSleepDurations configures the sleep durations and number of retries after Lease Get returns Rejected status default is the value of DefaultSleepDurations()

type Stats

type Stats struct {
	HitCount  uint64
	FillCount uint64 // can also be interpreted as the miss count

	LeaseGetError uint64 // lease get error count

	FirstRejectedCount  uint64
	SecondRejectedCount uint64
	ThirdRejectedCount  uint64
	TotalRejectedCount  uint64

	TotalBytesRecv uint64
}

Stats ...

type Unmarshaler

type Unmarshaler[T any] func(data []byte) (T, error)

Unmarshaler transforms raw bytes from memcached servers to the correct type

type Value

type Value interface {
	Marshal() ([]byte, error)
}

Value is the value constraint

Jump to

Keyboard shortcuts

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