cache

package
v0.5.0-beta2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSupportCategory error = errors.New("not support category")
	ErrNotFound           error = errors.New("not found")
	ErrStoreLimited       error = errors.New("store limited")
)

Functions

This section is empty.

Types

type CacheStore

type CacheStore interface {
	// update cached resource
	UpdateCache(ctx context.Context, request Request, data Resource) (*Resource, error)

	// find resource in cache, return nil if not in cache
	FindFromCache(ctx context.Context, request Request) (*Resource, error)

	// disable cache
	DisableCache(ctx context.Context, request Request) error
}

cache repository used for cache store

type ErrHandler

type ErrHandler interface {
	// go through on cache err, store limiter will also work
	ThroughOnCacheErr(ctx context.Context, resource Request, err error) (goThrough bool)

	//data downgrading if find data has err
	Downgrading(ctx context.Context, resource Request, err error) (*Resource, error)
}

type Handler

type Handler interface {
	CacheStore
	SourceDataStore
	ErrHandler
}

type Middle

type Middle interface {
	WrapCache(store CacheStore) CacheStore
	WrapStore(store SourceDataStore) SourceDataStore
	WrapErrHandler(handler ErrHandler) ErrHandler
}

func NewRecorderMid

func NewRecorderMid(factory record.Factory) Middle

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository(handler ResourceHandler, logger *zap.Logger, middle ...Middle) *Repository

func (*Repository) Disable

func (rep *Repository) Disable(ctx context.Context, request Request, force bool) error

Disable disable the cache.

func (*Repository) Fetch

func (rep *Repository) Fetch(ctx context.Context, id Request, syncCache bool) (*Resource, error)

Fetch resource from source data store. Update cache if syncCache is true. This method can be used to update cache!

func (*Repository) Find

func (rep *Repository) Find(ctx context.Context, request Request) (*Resource, error)

find resource in cache first, if not exists than read from store and update to cache

func (*Repository) SyncCache

func (rep *Repository) SyncCache(ctx context.Context, request Request) (*Resource, error)

enforce sync cache from store

type Request

type Request struct {
	ID     interface{}
	Params map[string]string
}

type Resource

type Resource struct {
	Meta map[string]string
	Data interface{}
}

type ResourceHandler

type ResourceHandler struct {
	ThroughLimit            *rate.Limiter
	FetchFromStoreFunc      func(ctx context.Context, request Request) (*Resource, error)
	UpdateCacheFunc         func(ctx context.Context, request Request, data Resource) (*Resource, error)
	FindFromCacheFunc       func(ctx context.Context, request Request) (*Resource, error)
	DisableCacheFunc        func(ctx context.Context, request Request) error
	ThroughOnCacheErrFunc   func(ctx context.Context, resource Request, err error) (goThrough bool)   // go through on cache err, store limiter will also work
	Downgrade               func(ctx context.Context, resource Request, err error) (*Resource, error) //data downgrading
	BeforeResourceCacheFunc func(key Request)
}

func (*ResourceHandler) BeforeResourceCache

func (h *ResourceHandler) BeforeResourceCache(key Request)

func (ResourceHandler) DisableCache

func (h ResourceHandler) DisableCache(ctx context.Context, id Request) error

func (ResourceHandler) Downgrading

func (h ResourceHandler) Downgrading(ctx context.Context, key Request, err error) (*Resource, error)

data downgrading if find data has err

func (ResourceHandler) FetchFromStore

func (h ResourceHandler) FetchFromStore(ctx context.Context, id Request) (*Resource, error)

func (ResourceHandler) FindFromCache

func (h ResourceHandler) FindFromCache(ctx context.Context, id Request) (*Resource, error)

func (ResourceHandler) ThroughOnCacheErr

func (h ResourceHandler) ThroughOnCacheErr(ctx context.Context, key Request, err error) (goThrough bool)

go through on cache err, store limiter will also work

func (ResourceHandler) UpdateCache

func (h ResourceHandler) UpdateCache(ctx context.Context, request Request, data Resource) (*Resource, error)

type SourceDataStore

type SourceDataStore interface {
	// return nil, if not exists
	FetchFromStore(ctx context.Context, id Request) (*Resource, error)
}

source data store

Jump to

Keyboard shortcuts

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