entity

package
v0.43.10 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 13 Imported by: 0

README

简述

适合读多写少的场景

特点

  • 多级数据:内存缓存(big-cache),远端缓存(redis),数据库(mysql),自定义获取(未实现)
  • 数据一致性:所有内存缓存通过pub-sub保障一致性,更新内存缓存时也同时更新远端缓存

策略

可以自由组合数据获取策略

  • 内存缓存 + 远端缓存: 本地查看排行榜,本地短暂缓存排行榜数据
  • 内存缓存 + 数据库: 用于本地直接操作数据库时,缓存中无数据时从数据库获取
  • 内存缓存 + 自定义:用于专用数据库服务器操作数据时,缓存中无数据时使用自定义函数从数据库服务器拉取数据

目录

替换

替换内存缓存

替换数据库

自定义获取

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound 数据未找到
	ErrNotFound = errors.New("data not found")
	// ErrEmptyPlaceholder 数据为空
	ErrEmptyPlaceholder = errors.New("empty placeholder")
	// ErrTimeout 超时
	ErrTimeout = errors.New("timeout")
	// ErrResultIndexInvalid 无效索引
	ErrResultIndexInvalid = errors.New("index out of range")
	// ErrResultIdNotFound ID 未找到
	ErrResultIdNotFound = errors.New("id not found")
	// ErrIDCantBeNull ID 不可以为空
	ErrIDCantBeNull = errors.New("id cant be null")
)

Functions

This section is empty.

Types

type DeleteHandler added in v0.41.9

type DeleteHandler func(out interface{}, id ...uint64) error

DeleteHandler 删除函数

type Entity added in v0.41.9

type Entity interface {
	// Build 内部有一些处理
	Build()

	// Unmarshal 解码
	Unmarshal(in []byte, out interface{}) error

	// Get 根据主键获取数据
	Get(out interface{}, id uint64) error

	// GetWithQuery 根据主键获取数据
	// query 自定义查询
	GetWithQuery(out interface{}, id uint64, query QueryHandler) error

	// MGet 根据主键批量获取数据
	MGet(out interface{}, ids ...uint64) (*Result, error)

	// Set 缓存数据
	Set(in interface{}, id uint64) error

	// Update 更新数据库,更新缓存
	Update(model interface{}, id uint64) error

	// Delete 删除数据库,删除缓存
	Delete(model interface{}, id uint64) error

	// MDelete 批量删除数据库,删除缓存
	MDelete(model interface{}, ids ...uint64) error

	WithCodec(codec zerocodec.Codec) Entity
	WithTimeout(timeout time.Duration) Entity
	WithNotFoundExipred(expired time.Duration) Entity
	WithReadDB(dbs ...WrapReadDB) Entity
	WithWriteDB(db WrapWriteDB) Entity
	WithLocalCache(localCache WrapCache) Entity
	WithRemoteCache(remoteCache WrapCache) Entity
	WithCustomQueryHandler(handler QueryHandler) Entity
	WithCustomUpdateHandler(handler UpdateHandler) Entity
	WithCustomDeleteHandler(handler DeleteHandler) Entity
}

Entity 实体

func New added in v0.41.9

func New(st *Stat, logger zerologger.Logger, codec zerocodec.Codec) Entity

New 创建一个实体管理器

type QueryHandler

type QueryHandler func(out interface{}, ids ...uint64) ([]uint64, []interface{}, error)

QueryHandler 查询函数

type Result added in v0.41.9

type Result struct {
	Vals [][]byte
	Errs []error
}

Results 查询结果

func (*Result) Index added in v0.41.9

func (r *Result) Index(idx int) ([]byte, error)

Index 根据索引获取结果

func (*Result) Len added in v0.41.9

func (r *Result) Len() int

type Stat

type Stat struct {
	Name string
	// contains filtered or unexported fields
}

Stat 统计

func NewStat

func NewStat(name string, handler StatHandler) *Stat

type StatHandler added in v0.41.9

type StatHandler func(localHit, localMiss, remoteHit, remoteMiss, dbFails, customFails uint64)

StatHandler 统计处理函数

type UpdateHandler added in v0.41.9

type UpdateHandler func(out interface{}, id ...uint64) error

UpdateHandler 更新函数

type Value added in v0.41.9

type Value struct {
	ID  uint64
	Val []byte
	Err error
}

type WrapCache

type WrapCache interface {
	Get(id uint64) ([]byte, error)
	MGet(ids ...uint64) ([]*Value, error)
	Set(id uint64, in []byte) error
	MSet(ids []uint64, datas [][]byte) error
	Delete(id uint64) error
	MDelete(ids ...uint64) error
	ErrNotFound() error
}

WrapCache 封装缓存

type WrapReadDB added in v0.41.9

type WrapReadDB interface {
	Get(out interface{}, id uint64) error
	MGet(out interface{}, ids ...uint64) ([]uint64, []interface{}, error)
	ErrNotFound() error
}

WrapReadDB 封装读数据库

type WrapWriteDB added in v0.41.9

type WrapWriteDB interface {
	Update(in interface{}) error
	Delete(model interface{}, id uint64) error
	MDelete(model interface{}, ids ...uint64) error
	ErrNotFound() error
}

WrapWriteDB 封装写数据库

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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