cache

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 10 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, data Resource) error

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

	// disable cache
	DisableCache(ctx context.Context, id ResourceKey) 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 ResourceKey, err error) (goThrough bool)

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

type Handler

type Handler struct {
	Key ResourceKey
	// contains filtered or unexported fields
}

type PrometheusMonitor

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

func NewPrometheusMonitor

func NewPrometheusMonitor(name string) *PrometheusMonitor

func (*PrometheusMonitor) AddFindRecord

func (monitor *PrometheusMonitor) AddFindRecord(key ResourceKey, hit bool, downgrade bool, err error, duration time.Duration)

func (*PrometheusMonitor) Controller

func (monitor *PrometheusMonitor) Controller() prometheus.Collector

type ReadProcessType

type ReadProcessType int
const (
	ReadFromCache     ReadProcessType = 1
	ReadFromStore     ReadProcessType = 1
	ReadFromDowngrade ReadProcessType = 1
)

type Repository

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

func NewRepository

func NewRepository(handler *ResourceHandlers, monitor RepositoryMonitor, logger *zap.Logger, throughLimit rate.Limit) *Repository

func (*Repository) Disable

func (rep *Repository) Disable(ctx context.Context, id ResourceKey, force bool) error

Disable disable the cache.

func (*Repository) Fetch

func (rep *Repository) Fetch(ctx context.Context, id ResourceKey, 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, id ResourceKey) (*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, id ResourceKey) (*Resource, error)

enforce sync cache from store

type RepositoryHandler

type RepositoryHandler interface {
	SourceDataStore
	CacheStore
	RepositoryMonitor
	RepositoryHook
}

gather business logic into handler

type RepositoryHook

type RepositoryHook interface {
	BeforeResourceCache(key ResourceKey)
}

monitor interface

type RepositoryMonitor

type RepositoryMonitor interface {
	AddFindRecord(key ResourceKey, hit bool, downgrade bool, err error, total time.Duration)
}

monitor interface

type Resource

type Resource struct {
	Key  ResourceKey
	Data interface{}
}

type ResourceHandler

type ResourceHandler struct {
	Category                string
	FetchFromStoreFunc      func(ctx context.Context, id ResourceKey) (*Resource, error)
	UpdateCacheFunc         func(ctx context.Context, data Resource) error
	FindFromCacheFunc       func(ctx context.Context, id ResourceKey) (*Resource, error)
	DisableCache            func(ctx context.Context, id ResourceKey) error
	ThroughOnCacheErr       func(ctx context.Context, resource ResourceKey, err error) (goThrough bool)   // go through on cache err, store limiter will also work
	DowngradeOnErr          func(ctx context.Context, resource ResourceKey, err error) (*Resource, error) //data downgrading
	BeforeResourceCacheFunc func(key ResourceKey)
}

type ResourceHandlers

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

func NewResourceHandlers

func NewResourceHandlers() *ResourceHandlers

func (*ResourceHandlers) BeforeResourceCache

func (handlers *ResourceHandlers) BeforeResourceCache(key ResourceKey)

func (*ResourceHandlers) DisableCache

func (handlers *ResourceHandlers) DisableCache(ctx context.Context, id ResourceKey) error

func (*ResourceHandlers) Downgrading

func (handlers *ResourceHandlers) Downgrading(ctx context.Context, key ResourceKey, err error) (*Resource, error)

data downgrading if find data has err

func (*ResourceHandlers) FetchFromStore

func (handlers *ResourceHandlers) FetchFromStore(ctx context.Context, id ResourceKey) (*Resource, error)

func (*ResourceHandlers) FindFromCache

func (handlers *ResourceHandlers) FindFromCache(ctx context.Context, id ResourceKey) (*Resource, error)

func (*ResourceHandlers) SetHandler

func (handlers *ResourceHandlers) SetHandler(handler ResourceHandler)

func (*ResourceHandlers) ThroughOnCacheErr

func (handlers *ResourceHandlers) ThroughOnCacheErr(ctx context.Context, key ResourceKey, err error) (goThrough bool)

go through on cache err, store limiter will also work

func (*ResourceHandlers) UpdateCache

func (handlers *ResourceHandlers) UpdateCache(ctx context.Context, data Resource) error

type ResourceKey

type ResourceKey struct {
	Category string // used for category, helper field
	ID       string
}

type SourceDataStore

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

source data store

type Span

type Span struct {
	Hit             bool
	ReadProcessType ReadProcessType
	CacheRead       time.Duration
	StoreRead       time.Duration
	DowngradeRead   time.Duration
	WaitDuration    time.Duration
}

type Tracers

type Tracers interface {
	OpenTracer(error) Span
}

Jump to

Keyboard shortcuts

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