chain

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 6 Imported by: 0

README

trcache Chain GoDoc

Installation

go get github.com/rrgmc/trcache/cache/chain

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidStrategyResult = errors.New("invalid strategy result")
)

Functions

func WithDefaultStrategyCallback

func WithDefaultStrategyCallback[K comparable, V any](callback StrategyCallback) trcache.RootOption

WithDefaultStrategyCallback sets a callback function to receive strategy results.

func WithDeleteStrategy

func WithDeleteStrategy[K comparable, V any](deleteStrategy DeleteStrategy[K, V]) trcache.RootOption

WithDeleteStrategy sets the DeleteStrategy to use for the chain operation. The default is DeleteStrategyDeleteAll.

func WithDeleteStrategyCallback

func WithDeleteStrategyCallback[K comparable, V any](callback StrategyCallback) trcache.DeleteOption

WithDeleteStrategyCallback sets a callback function to receive strategy results.

func WithGetSetOptions

func WithGetSetOptions[K comparable, V any](options ...trcache.SetOption) trcache.GetOption

WithGetSetOptions adds options to the [Cache.Set] call done after one of the [Cache.Get] function calls succeeds.

func WithGetStrategy

func WithGetStrategy[K comparable, V any](getStrategy GetStrategy[K, V]) trcache.RootOption

WithGetStrategy sets the GetStrategy to use for the chain operation. The default is GetStrategyGetFirstSetPrevious.

func WithGetStrategyCallback

func WithGetStrategyCallback[K comparable, V any](callback StrategyCallback) trcache.GetOption

WithGetStrategyCallback sets a callback function to receive strategy results.

func WithSetStrategy

func WithSetStrategy[K comparable, V any](setStrategy SetStrategy[K, V]) trcache.RootOption

WithSetStrategy sets the SetStrategy to use for the chain operation. The default is SetStrategySetAll.

func WithSetStrategyCallback

func WithSetStrategyCallback[K comparable, V any](callback StrategyCallback) trcache.SetOption

WithSetStrategyCallback sets a callback function to receive strategy results.

Types

type Chain

type Chain[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any](cache []trcache.Cache[K, V],
	options ...trcache.RootOption) (*Chain[K, V], error)

func (*Chain[K, V]) Delete

func (c *Chain[K, V]) Delete(ctx context.Context, key K,
	options ...trcache.DeleteOption) error

func (*Chain[K, V]) Get

func (c *Chain[K, V]) Get(ctx context.Context, key K,
	options ...trcache.GetOption) (V, error)

func (*Chain[K, V]) Name

func (c *Chain[K, V]) Name() string

func (*Chain[K, V]) Set

func (c *Chain[K, V]) Set(ctx context.Context, key K, value V,
	options ...trcache.SetOption) error

type ChainError

type ChainError struct {
	ErrType ChainErrorType
	Message string
	Err     error
}

func NewChainError

func NewChainError(errType ChainErrorType, message string, err error) ChainError

func (ChainError) As

func (e ChainError) As(target any) bool

func (ChainError) Error

func (e ChainError) Error() string

func (ChainError) Is

func (e ChainError) Is(err error) bool

type ChainErrorType

type ChainErrorType int
const (
	ChainErrorTypeError ChainErrorType = iota
	ChainErrorTypeIncomplete
)

type ChainRefresh

type ChainRefresh[K comparable, V any] struct {
	*Chain[K, V]
	// contains filtered or unexported fields
}

func NewRefresh

func NewRefresh[K comparable, V any](cache []trcache.Cache[K, V],
	options ...trcache.RootOption) (*ChainRefresh[K, V], error)

func (*ChainRefresh[K, V]) GetOrRefresh

func (c *ChainRefresh[K, V]) GetOrRefresh(ctx context.Context, key K, options ...trcache.RefreshOption) (V, error)

type DeleteStrategy

type DeleteStrategy[K comparable, V any] interface {
	DeleteLoopOrder(ctx context.Context) StrategyLoopOrder
	BeforeDelete(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) DeleteStrategyBeforeResult
	AfterDelete(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, err error) DeleteStrategyAfterResult
}

DeleteStrategy is the strategy to be used on Chain.Delete.

Chain.Delete uses this logic:

  • **loop** on the list of caches from 0 to len().
  • call [DeleteStrategy.BeforeDelete] on the current cache.
  • if SKIP, stop processing this cache and go to next loop.
  • if DELETE, continue processing.
  • call [Cache.Delete] and store the error
  • call [DeleteStrategy.AfterDelete] with the result of the previous call (including the error).
  • if RETURN, return immediately the previous call error result.
  • if CONTINUEWITHERROR, continue to the next loop item, appending the error to a list of errors to return.
  • if CONTINUE, continue to the next loop item.
  • at the end, return the error list if any.

func NewDefaultDeleteStrategy

func NewDefaultDeleteStrategy[K comparable, V any]() DeleteStrategy[K, V]

type DeleteStrategyAfterResult

type DeleteStrategyAfterResult int
const (
	DeleteStrategyAfterResultCONTINUE DeleteStrategyAfterResult = iota
	DeleteStrategyAfterResultRETURN
	DeleteStrategyAfterResultCONTINUEWITHERROR
)

type DeleteStrategyBeforeResult

type DeleteStrategyBeforeResult int
const (
	DeleteStrategyBeforeResultDELETE DeleteStrategyBeforeResult = iota
	DeleteStrategyBeforeResultSKIP
)

type DeleteStrategyDeleteAll

type DeleteStrategyDeleteAll[K comparable, V any] struct {
}

func (DeleteStrategyDeleteAll[K, V]) AfterDelete

func (f DeleteStrategyDeleteAll[K, V]) AfterDelete(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, err error) DeleteStrategyAfterResult

func (DeleteStrategyDeleteAll[K, V]) BeforeDelete

func (f DeleteStrategyDeleteAll[K, V]) BeforeDelete(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) DeleteStrategyBeforeResult

func (DeleteStrategyDeleteAll[K, V]) DeleteLoopOrder

func (f DeleteStrategyDeleteAll[K, V]) DeleteLoopOrder(ctx context.Context) StrategyLoopOrder

type DeleteStrategyFunc

type DeleteStrategyFunc[K comparable, V any] struct {
	DeleteLoopOrderFn func(ctx context.Context) StrategyLoopOrder
	BeforeDeleteFn    func(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) DeleteStrategyBeforeResult
	AfterDeleteFn     func(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, err error) DeleteStrategyAfterResult
}

func (DeleteStrategyFunc[K, V]) AfterDelete

func (f DeleteStrategyFunc[K, V]) AfterDelete(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, err error) DeleteStrategyAfterResult

func (DeleteStrategyFunc[K, V]) BeforeDelete

func (f DeleteStrategyFunc[K, V]) BeforeDelete(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) DeleteStrategyBeforeResult

func (DeleteStrategyFunc[K, V]) DeleteLoopOrder

func (f DeleteStrategyFunc[K, V]) DeleteLoopOrder(ctx context.Context) StrategyLoopOrder

type GetStrategy

type GetStrategy[K comparable, V any] interface {
	GetLoopOrder(ctx context.Context) StrategyLoopOrder
	BeforeGet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) GetStrategyBeforeResult
	AfterGet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterResult
	SetLoopOrder(ctx context.Context) StrategyLoopOrder
	BeforeSet(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V) GetStrategyBeforeSetResult
	AfterSet(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterSetResult
}

GetStrategy is the strategy to be used on Chain.Get.

Chain.Get uses this logic:

  • **loop** on the list of caches from 0 to len().
  • call [GetStrategy.BeforeGet] on the current cache.
  • if SKIP, stop processing this cache and go to next loop.
  • if GET, continue processing.
  • call [Cache.Get] and store the result / error.
  • call [GetStrategy.AfterGet] with the result of the previous call (including the error).
  • if SKIP, continue to the next loop item.
  • if RETURN, set this result as the current function result (even if it was an error) and exit the loop.
  • **if no result was found**, or the last returned result is an error, return the error to the user.
  • **loop** on the list of cache from len() to 0 (backwards)
  • call [GetStrategy.BeforeSet] on the current cache.
  • if SKIP, stop processing this cache and go to next loop.
  • if SET, continue processing.
  • call [Cache.Set] with the result found in the "Get" loop.
  • call [GetStrategy.AfterSet] with the result of the previous call (including the error).
  • if RETURN, return immediately with the result and error of the last "Set" call.
  • if CONTINUE, continue to the next loop item.
  • at the end, return the found result and error.

func NewDefaultGetStrategy

func NewDefaultGetStrategy[K comparable, V any]() GetStrategy[K, V]

type GetStrategyAfterResult

type GetStrategyAfterResult int
const (
	GetStrategyAfterResultRETURN GetStrategyAfterResult = iota
	GetStrategyAfterResultSKIP
)

type GetStrategyAfterSetResult

type GetStrategyAfterSetResult int
const (
	GetStrategyAfterSetResultCONTINUE GetStrategyAfterSetResult = iota
	GetStrategyAfterSetResultRETURN
)

type GetStrategyBeforeResult

type GetStrategyBeforeResult int
const (
	GetStrategyBeforeResultGET GetStrategyBeforeResult = iota
	GetStrategyBeforeResultSKIP
)

type GetStrategyBeforeSetResult

type GetStrategyBeforeSetResult int
const (
	GetStrategyBeforeSetResultSET GetStrategyBeforeSetResult = iota
	GetStrategyBeforeSetResultSKIP
)

type GetStrategyFunc

type GetStrategyFunc[K comparable, V any] struct {
	GetLoopOrderFn func(ctx context.Context) StrategyLoopOrder
	BeforeGetFn    func(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) GetStrategyBeforeResult
	AfterGetFn     func(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterResult
	SetLoopOrderFn func(ctx context.Context) StrategyLoopOrder
	BeforeSetFn    func(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V) GetStrategyBeforeSetResult
	AfterSetFn     func(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterSetResult
}

func (GetStrategyFunc[K, V]) AfterGet

func (f GetStrategyFunc[K, V]) AfterGet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterResult

func (GetStrategyFunc[K, V]) AfterSet

func (f GetStrategyFunc[K, V]) AfterSet(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterSetResult

func (GetStrategyFunc[K, V]) BeforeGet

func (f GetStrategyFunc[K, V]) BeforeGet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) GetStrategyBeforeResult

func (GetStrategyFunc[K, V]) BeforeSet

func (f GetStrategyFunc[K, V]) BeforeSet(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V) GetStrategyBeforeSetResult

func (GetStrategyFunc[K, V]) GetLoopOrder

func (f GetStrategyFunc[K, V]) GetLoopOrder(ctx context.Context) StrategyLoopOrder

func (GetStrategyFunc[K, V]) SetLoopOrder

func (f GetStrategyFunc[K, V]) SetLoopOrder(ctx context.Context) StrategyLoopOrder

type GetStrategyGetFirstSetPrevious

type GetStrategyGetFirstSetPrevious[K comparable, V any] struct {
}

func (GetStrategyGetFirstSetPrevious[K, V]) AfterGet

func (f GetStrategyGetFirstSetPrevious[K, V]) AfterGet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterResult

func (GetStrategyGetFirstSetPrevious[K, V]) AfterSet

func (f GetStrategyGetFirstSetPrevious[K, V]) AfterSet(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) GetStrategyAfterSetResult

func (GetStrategyGetFirstSetPrevious[K, V]) BeforeGet

func (f GetStrategyGetFirstSetPrevious[K, V]) BeforeGet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K) GetStrategyBeforeResult

func (GetStrategyGetFirstSetPrevious[K, V]) BeforeSet

func (f GetStrategyGetFirstSetPrevious[K, V]) BeforeSet(ctx context.Context, gotCacheIdx, cacheIdx int, cache trcache.Cache[K, V], key K, value V) GetStrategyBeforeSetResult

func (GetStrategyGetFirstSetPrevious[K, V]) GetLoopOrder

func (GetStrategyGetFirstSetPrevious[K, V]) SetLoopOrder

type SetStrategy

type SetStrategy[K comparable, V any] interface {
	SetLoopOrder(ctx context.Context) StrategyLoopOrder
	BeforeSet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V) SetStrategyBeforeResult
	AfterSet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) SetStrategyAfterResult
}

SetStrategy is the strategy to be used on Chain.Set.

Chain.Set uses this logic:

  • **loop** on the list of caches from 0 to len().
  • call [SetStrategy.BeforeSet] on the current cache.
  • if SKIP, stop processing this cache and go to next loop.
  • if SET, continue processing.
  • call [Cache.Set] and store the error.
  • call [SetStrategy.AfterSet] with the result of the previous call (including the error).
  • if RETURN, return immediately the previous call error result.
  • if CONTINUEWITHERROR, continue to the next loop item, appending the error to a list of errors to return.
  • if CONTINUE, continue to the next loop item.
  • at the end, return the error list if any.

func NewDefaultSetStrategy

func NewDefaultSetStrategy[K comparable, V any]() SetStrategy[K, V]

type SetStrategyAfterResult

type SetStrategyAfterResult int
const (
	SetStrategyAfterResultCONTINUE SetStrategyAfterResult = iota
	SetStrategyAfterResultRETURN
	SetStrategyAfterResultCONTINUEWITHERROR
)

type SetStrategyBeforeResult

type SetStrategyBeforeResult int
const (
	SetStrategyBeforeResultSET SetStrategyBeforeResult = iota
	SetStrategyBeforeResultSKIP
)

type SetStrategyFunc

type SetStrategyFunc[K comparable, V any] struct {
	SetLoopOrderFn func(ctx context.Context) StrategyLoopOrder
	BeforeSetFn    func(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V) SetStrategyBeforeResult
	AfterSetFn     func(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) SetStrategyAfterResult
}

func (SetStrategyFunc[K, V]) AfterSet

func (f SetStrategyFunc[K, V]) AfterSet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) SetStrategyAfterResult

func (SetStrategyFunc[K, V]) BeforeSet

func (f SetStrategyFunc[K, V]) BeforeSet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V) SetStrategyBeforeResult

func (SetStrategyFunc[K, V]) SetLoopOrder

func (f SetStrategyFunc[K, V]) SetLoopOrder(ctx context.Context) StrategyLoopOrder

type SetStrategySetAll

type SetStrategySetAll[K comparable, V any] struct {
}

func (SetStrategySetAll[K, V]) AfterSet

func (f SetStrategySetAll[K, V]) AfterSet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V, err error) SetStrategyAfterResult

func (SetStrategySetAll[K, V]) BeforeSet

func (f SetStrategySetAll[K, V]) BeforeSet(ctx context.Context, cacheIdx int, cache trcache.Cache[K, V], key K, value V) SetStrategyBeforeResult

func (SetStrategySetAll[K, V]) SetLoopOrder

func (f SetStrategySetAll[K, V]) SetLoopOrder(ctx context.Context) StrategyLoopOrder

type StrategyCallback

type StrategyCallback interface {
	Get(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result GetStrategyAfterResult)
	GetSet(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result GetStrategyAfterSetResult)
	Set(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result SetStrategyAfterResult)
	Delete(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result DeleteStrategyAfterResult)
}

type StrategyCallbackFunc

type StrategyCallbackFunc struct {
	GetFn    func(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result GetStrategyAfterResult)
	GetSetFn func(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result GetStrategyAfterSetResult)
	SetFn    func(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result SetStrategyAfterResult)
	DeleteFn func(ctx context.Context, cacheIdx int, cacheName string, key any, err error, result DeleteStrategyAfterResult)
}

func (*StrategyCallbackFunc) Delete

func (s *StrategyCallbackFunc) Delete(ctx context.Context, cacheIdx int, cacheName string, key any, err error,
	result DeleteStrategyAfterResult)

func (*StrategyCallbackFunc) Get

func (s *StrategyCallbackFunc) Get(ctx context.Context, cacheIdx int, cacheName string, key any, err error,
	result GetStrategyAfterResult)

func (*StrategyCallbackFunc) GetSet

func (s *StrategyCallbackFunc) GetSet(ctx context.Context, cacheIdx int, cacheName string, key any, err error,
	result GetStrategyAfterSetResult)

func (*StrategyCallbackFunc) Set

func (s *StrategyCallbackFunc) Set(ctx context.Context, cacheIdx int, cacheName string, key any, err error,
	result SetStrategyAfterResult)

type StrategyLoopOrder

type StrategyLoopOrder int
const (
	StrategyLoopOrderFORWARD StrategyLoopOrder = iota
	StrategyLoopOrderBACKWARD
)

Jump to

Keyboard shortcuts

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