redmos

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Timeout  = time.Second * 30 // redis超時時間
	RedisNil = ""               // redis回應空字串, 通常在GET命令找不到索引時, 會以此字串回報給使用者
	RedisOk  = "OK"             // redis回應完成, 通常在SET命令順利完成後, 會以此字串回報給使用者
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Behave

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

Behave 行為資料

func (*Behave) Ctx

func (this *Behave) Ctx() context.Context

Ctx 取得ctx物件

func (*Behave) Initialize

func (this *Behave) Initialize(ctx context.Context, major MajorSubmit, minor *MinorSubmit)

Initialize 初始處理

func (*Behave) Major

func (this *Behave) Major() MajorSubmit

Major 取得主要執行物件

func (*Behave) Minor

func (this *Behave) Minor() *MinorSubmit

Minor 取得次要執行物件

type Behavior

type Behavior interface {
	// Initialize 初始處理
	Initialize(ctx context.Context, major MajorSubmit, minor *MinorSubmit)

	// Prepare 準備處理
	Prepare() error

	// Complete 完成處理
	Complete() error
}

Behavior 行為介面, 當建立行為時, 需要實現此介面, 建議可以把 Behave 組合進行為結構中, 可以省去初始處理的實作; 設計行為時, 有以下的設計規範

  • 主要資料庫的情況下: 由於主要資料庫會用管線機制執行, 因此通常會在 Prepare 新增管線命令, 然後在 Complete 檢查執行是否符合預期
  • 次要資料庫的情況下: 由於次要資料庫會以常規方式執行, 因此通常 Prepare 不會有內容, 然後在 Complete 執行資料庫命令並且檢查執行是否符合預期
  • 錯誤處理: 當資料庫失敗時才會回傳錯誤, 若是邏輯錯誤(例如資料不存在), 就不應該回傳錯誤, 而是把結果記錄下來提供外部使用

type Del

type Del struct {
	Behave             // 行為物件
	MajorEnable bool   // 啟用主要資料庫
	MinorEnable bool   // 啟用次要資料庫
	Meta        Metaer // 元資料
	Key         string // 索引值
	// contains filtered or unexported fields
}

Del 刪除行為, 以索引字串與資料到主要/次要資料庫中刪除資料, 使用上有以下幾點須注意

  • 執行前設定好 MajorEnable, MinorEnable
  • 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
  • 執行前設定好 Key 並且不能為空字串

func (*Del) Complete

func (this *Del) Complete() error

Complete 完成處理

func (*Del) Prepare

func (this *Del) Prepare() error

Prepare 前置處理

type Exist

type Exist struct {
	Behave          // 行為物件
	Meta   Metaer   // 元資料
	Key    []string // 索引列表
	Count  int      // 存在的索引數量
	// contains filtered or unexported fields
}

Exist 查詢行為, 以索引列表到主要資料庫中查詢索引是否存在, 使用上有以下幾點須注意

  • 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
  • 執行前設定好 Key 並且不能為空列表
  • 執行後可用 Count 來取得存在的索引數量

func (*Exist) Complete

func (this *Exist) Complete() error

Complete 完成處理

func (*Exist) Prepare

func (this *Exist) Prepare() error

Prepare 前置處理

type Get

type Get[T any] struct {
	Behave             // 行為物件
	MajorEnable bool   // 啟用主要資料庫
	MinorEnable bool   // 啟用次要資料庫
	Meta        Metaer // 元資料
	Key         string // 索引值
	Data        *T     // 資料物件
	// contains filtered or unexported fields
}

Get 取值行為, 以索引字串到主要/次要資料庫中取得資料, 使用上有以下幾點須注意

  • 需要事先建立好資料結構, 並填寫到泛型類型T中, 請不要填入指標類型
  • 執行前設定好 MajorEnable, MinorEnable
  • 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
  • 執行前設定好 Key 並且不能為空字串
  • 執行前設定好 Data, 如果為nil, 則內部程序會自己建立
  • 執行後可用 Data 來取得資料

func (*Get[T]) Complete

func (this *Get[T]) Complete() error

Complete 完成處理

func (*Get[T]) Prepare

func (this *Get[T]) Prepare() error

Prepare 前置處理

type Incr

type Incr struct {
	Behave                // 行為物件
	MinorEnable bool      // 啟用次要資料庫
	Meta        Metaer    // 元資料
	Key         string    // 索引值
	Data        *IncrData // 資料物件
	// contains filtered or unexported fields
}

Incr 遞增行為, 以索引字串到主要/次要資料庫中遞增數值, 使用上有以下幾點須注意

  • 執行前設定好 MinorEnable. 請注意! 遞增行為必定會在主資料庫中執行, 因此無法禁用主資料庫
  • 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
  • 執行前設定好 Key 並且不能為空字串
  • 執行前設定好 Data 並且不能為空物件
  • 由於遞增行為是以int64來運作, 因此使用時可能需要轉換

func (*Incr) Complete

func (this *Incr) Complete() error

Complete 完成處理

func (*Incr) Prepare

func (this *Incr) Prepare() error

Prepare 前置處理

type IncrData

type IncrData struct {
	Incr  int64 // 遞增數值
	Value int64 // 遞增結果
}

IncrData 遞增資料

type Index

type Index struct {
	Behave        // 行為物件
	Meta   Metaer // 元資料
	Name   string // 索引名稱
	Order  int    // 排序方向, 1表示順序, -1表示逆序
	Unique bool   // 是否唯一索引, 唯一索引的情況下, 索引值不允許重複
}

Index 建立索引行為, 到次要資料庫中建立索引, 使用上有以下幾點須注意

  • 只有次要資料庫的操作會被索引影響, 當查詢的欄位符合索引時會自動生效
  • 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
  • 執行前設定好 Name, Order, Unique 並且要符合規範

func (*Index) Complete

func (this *Index) Complete() error

Complete 完成處理

func (*Index) Prepare

func (this *Index) Prepare() error

Prepare 前置處理

type Lock

type Lock struct {
	Behave        // 行為物件
	Key    string // 索引值
	// contains filtered or unexported fields
}

Lock 鎖定行為, 以索引字串到主要資料庫中執行分布式鎖定, 避免同時執行客戶端動作, 使用上有以下幾點須注意

  • 執行前設定好 Key 並且不能為空字串
  • 鎖定完成後, 需要執行 Unlock 行為來解除鎖定
  • 鎖定後會在 Timeout 之後自動解鎖, 避免死鎖

func (*Lock) Complete

func (this *Lock) Complete() error

Complete 完成處理

func (*Lock) Prepare

func (this *Lock) Prepare() error

Prepare 前置處理

type Major

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

Major 主要資料庫, 內部用redis實現的資料庫組件, 包含以下功能

  • 取得執行物件: 取得資料庫執行器, 實際上就是redis管線
  • 取得客戶端物件: 取得原生資料庫執行器, 可用來執行更細緻的命令

func (*Major) Client

func (this *Major) Client() redis.UniversalClient

Client 取得客戶端物件

func (*Major) DropDB

func (this *Major) DropDB()

DropDB 清除資料庫

func (*Major) Submit

func (this *Major) Submit() MajorSubmit

Submit 取得執行物件

func (*Major) SwitchDB

func (this *Major) SwitchDB(dbID int) error

SwitchDB 切換資料庫, redis預設只能使用編號0~15的資料庫

type MajorSubmit

type MajorSubmit = redis.Pipeliner

MajorSubmit 資料庫執行器, 實際上就是redis管線

type Metaer

type Metaer interface {
	// MajorKey 取得主要資料庫索引字串
	MajorKey(key any) string

	// MinorKey 取得次要資料庫索引字串
	MinorKey(key any) string

	// MinorTable 取得次要資料庫表格名稱
	MinorTable() string

	// MinorField 取得次要資料庫索引欄位名稱
	MinorField() string
}

Metaer 元資料介面, 提供主要/次要資料庫操作時所需的必要資訊

type Minor

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

Minor 次要資料庫, 內部用mongo實現的資料庫組件, 包含以下功能

  • 取得執行物件: 取得資料庫執行器, 實際上就是mongo集合
  • 取得客戶端物件: 取得原生客戶端執行器, 可用來執行更細緻的命令
  • 取得資料庫物件: 取得原生資料庫執行器, 可用來執行更細緻的命令

func (*Minor) Client

func (this *Minor) Client() *mongo.Client

Client 取得客戶端物件

func (*Minor) Database

func (this *Minor) Database() *mongo.Database

Database 取得資料庫物件

func (*Minor) DropDB

func (this *Minor) DropDB()

DropDB 清除資料庫

func (*Minor) Submit

func (this *Minor) Submit() *MinorSubmit

Submit 取得執行物件

func (*Minor) SwitchDB

func (this *Minor) SwitchDB(dbName string) error

SwitchDB 切換資料庫

type MinorSubmit

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

MinorSubmit 資料庫執行器

func (*MinorSubmit) Collection

func (this *MinorSubmit) Collection(table string) *mongo.Collection

Collection 取得表格物件

func (*MinorSubmit) Exec

func (this *MinorSubmit) Exec(ctx context.Context) error

Exec 執行批量操作

func (*MinorSubmit) Operate

func (this *MinorSubmit) Operate(table string, operate mongo.WriteModel) *MinorSubmit

Operate 新增批量操作

type Mixed

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

Mixed 混合資料庫, 內部用主要與次要資料庫實現混合命令, 包含以下功能

  • 取得執行物件: 取得資料庫執行器

func (*Mixed) Submit

func (this *Mixed) Submit(ctx context.Context) *Submit

Submit 取得執行物件

type MongoURI

type MongoURI string

MongoURI mongo連接字串, 選項字串語法如下

  • mongodb://[username:password@]host1[:port1][,host2[:port2],...,hostN[:portN]]/[?options]]

語法中各組件的說明如下

  • mongodb:// @必選 必須是 'mongodb://'
  • [username:password@] @可選 指定連接時使用的帳號密碼
  • host1[:port1][,host2[:port2],...,hostN[:portN]] @必選 指定連接位址與埠號, 如果要連接到分片叢集的話, 就需要設定多組位址與埠號 如果省略埠號的話, 就會使用mongo預設的埠號27017
  • [?options] @可選 設定連接選項, 選項以'&'符號分隔, 例如: name=value&name=value

以下是連接選項說明, 選項中有關於時間值的單位都是毫秒

  • connectTimeoutMS 連接超時時間, 預設值為30000
  • timeoutMS 命令超時時間, 預設值為0, 0表示不會超時
  • maxIdleTimeMS 連接閒置時間, 連接如果閒置超過此時間將會被刪除, 預設值為0, 0表示不刪除
  • heartbeatFrequencyMS 心跳檢查時間, 預設值為10000
  • socketTimeoutMS socket操作超時時間, 超時會導致失敗, 預設值為0, 0表示不會超時
  • serverSelectionTimeoutMS 尋找伺服器超時時間
  • minPoolSize 最小連接池大小, 預設值為0
  • maxPoolSize 最大連接池大小, 預設值為100
  • replicaSet 集群的副本集名稱, 副本集中的所有節點必須具有相同的副本集名稱, 否則客戶端不會將它們視為副本集的一部分

也可以到以下網址查看選項詳細說明

func (MongoURI) Connect

func (this MongoURI) Connect(ctx context.Context) (client *mongo.Client, err error)

Connect 連接到資料庫

type RedisURI

type RedisURI string

RedisURI redis連接字串, 選項字串是仿造mongo配置字串做出來的, 選項字串語法如下

  • redisdb://[username:password@]host1:port1[,host2:port2,...,hostN:portN]/[?options]

語法中各組件的說明如下

  • redisdb:// @必選 必須是 'redisdb://'
  • [username:password@] @必選 指定連接時使用的帳號密碼
  • host1:port1[,host2:port2,...,hostN:portN]/ @必選 指定連接位址與埠號, 如果要連接到叢集或是哨兵的話, 就需要設定多組位址與埠號
  • [?options] @必選 設定連接選項, 選項以'&'符號分隔, 例如: name=value&name=value

以下是連接選項說明

  • clientName 設置連接名稱
  • dbid 設置連接資料庫編號
  • maxRetries 最大重試次數, 預設值為3, -1表示關閉此功能
  • minRetryBackoff 最小重試時間間隔, 預設值為8毫秒, -1表示關閉此功能
  • maxRetryBackoff 最大重試時間間隔, 預設值為512毫秒, -1表示關閉此功能
  • dialTimeout 連接超時時間, 預設值為5秒
  • readTimeout socket讀取超時時間, 超時會導致命令失敗, 預設值為3秒, -1表示關閉此功能
  • writeTimeout socket寫入超時時間, 超時會導致命令失敗, 預設值為3秒, -1表示關閉此功能
  • contextTimeoutEnabled 控制客戶端是否遵守上下文超時和截止日期
  • poolFIFO 設置連接池的類型, true表示FIFO池, false表示LIFO池 與LIFO相比, FIFO的開銷略高, 但它有助於更快地關閉空閒連接, 從而減少池大小
  • poolSize 最大socket連接數, 預設值為CPU數量*10
  • poolTimeout 連接池超時時間, 超時會導致從連接池中獲得連接失敗, 預設值為readTimeout + 1秒
  • minIdleConns 連接池中最小的閒置連接數量, 請注意建立新連接是很慢的
  • maxIdleConns 連接池中最大的閒置連接數量
  • connMaxIdleTime 連接閒置時間, 連接如果閒置超過此時間將會被刪除, 應該小於服務器的超時時間, 預設值為5分鐘, -1表示關閉此功能
  • connMaxLifetime 連接生存時間, 連接如果超過此時間將會被刪除, 預設值為不刪除
  • maxRedirects @叢集專用 當重定向時的最大重試次數, 預設值為8次
  • readOnly @叢集專用 控制是否在從屬節點上啟用只讀命令
  • routeByLatency @叢集專用 控制是否允許將只讀命令路由到最近的主節點或從節點, 它會自動啟用readOnly
  • routeRandomly @叢集專用 控制是否允許將只讀命令路由到隨機主節點或從節點, 它會自動啟用readOnly
  • masterName @哨兵專用 設定連接redis的主節點名稱, 當設定此項後, 就會改用哨兵模式來連接到redis

也可以到以下網址查看選項詳細說明

func (RedisURI) Connect

func (this RedisURI) Connect(ctx context.Context) (client redis.UniversalClient, err error)

Connect 連接到資料庫

type Redmomgr

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

Redmomgr 資料庫管理器, 用於管理雙層式資料庫架構

  • 主要資料庫: 用redis實作
  • 次要資料庫: 用mongo實作

當要新增資料庫時, 需要遵循以下流程:

  • 新增主要/次要資料庫
  • 新增混合資料庫: 這時會去取得先前新增的主要/次要資料庫, 並且將其`綁定`到混合資料庫中; 要注意的是, 混合資料庫必定是一個主要資料庫加上一個次要資料庫的組合, 若是缺少了任何一方則會失敗

若要執行資料庫操作時, 呼叫 Get... 系列函式來取得資料庫物件

func NewRedmomgr

func NewRedmomgr() *Redmomgr

NewRedmomgr 建立資料庫管理器

func (*Redmomgr) AddMajor

func (this *Redmomgr) AddMajor(majorName string, uri RedisURI) (major *Major, err error)

AddMajor 新增主要資料庫, 需要提供 RedisURI 來指定要連接的資料庫以及連接選項

func (*Redmomgr) AddMinor

func (this *Redmomgr) AddMinor(minorName string, uri MongoURI, dbName string) (minor *Minor, err error)

AddMinor 新增次要資料庫, 需要提供 MongoURI 來指定要連接的資料庫以及連接選項; 另外需要指定mongo資料庫名稱, 簡化後面取得執行器的流程, 但也因此限制次要資料庫不能在多個mongo資料庫間切換

func (*Redmomgr) AddMixed

func (this *Redmomgr) AddMixed(mixedName, majorName, minorName string) (mixed *Mixed, err error)

AddMixed 新增混合資料庫, 必須確保 majorName 與 minorName 必須是先前建立好的資料庫, 否則會失敗

func (*Redmomgr) Finalize

func (this *Redmomgr) Finalize()

Finalize 結束處理

func (*Redmomgr) GetMajor

func (this *Redmomgr) GetMajor(majorName string) *Major

GetMajor 取得主要資料庫

func (*Redmomgr) GetMinor

func (this *Redmomgr) GetMinor(minorName string) *Minor

GetMinor 取得次要資料庫

func (*Redmomgr) GetMixed

func (this *Redmomgr) GetMixed(mixedName string) *Mixed

GetMixed 取得混合資料庫

type Save

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

Save 儲存判斷資料, 用儲存旗標來判斷是否要儲存到主要/次要資料庫

func NewSave

func NewSave() *Save

NewSave 建立儲存判斷資料

func (*Save) GetSave

func (this *Save) GetSave() bool

GetSave 取得儲存旗標

func (*Save) SetSave

func (this *Save) SetSave()

SetSave 設定儲存旗標

type Saver

type Saver interface {
	// GetSave 取得儲存旗標
	GetSave() bool
}

Saver 儲存判斷介面

type Set

type Set[T any] struct {
	Behave                    // 行為物件
	MajorEnable bool          // 啟用主要資料庫
	MinorEnable bool          // 啟用次要資料庫
	Meta        Metaer        // 元資料
	Expire      time.Duration // 過期時間, 若為0表示不過期
	Key         string        // 索引值
	Data        *T            // 資料物件
	// contains filtered or unexported fields
}

Set 設值行為, 以索引字串與資料到主要/次要資料庫中儲存資料, 使用上有以下幾點須注意

  • 需要事先建立好資料結構, 並填寫到泛型類型T中, 請不要填入指標類型
  • 資料結構如果包含 Save 結構或是符合 Saver 介面, 會套用儲存判斷機制, 減少不必要的儲存操作
  • 資料結構的成員都需要設定好`bson:xxxxx`屬性
  • 執行前設定好 MajorEnable, MinorEnable
  • 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
  • 執行前設定好 Expire, 若不設置或是設置為0表示不過期
  • 執行前設定好 Key 並且不能為空字串
  • 執行前設定好 Data 並且不能為nil

func (*Set[T]) Complete

func (this *Set[T]) Complete() error

Complete 完成處理

func (*Set[T]) Prepare

func (this *Set[T]) Prepare() error

Prepare 前置處理

type SetNX

type SetNX[T any] struct {
	Behave                    // 行為物件
	MajorEnable bool          // 啟用主要資料庫
	MinorEnable bool          // 啟用次要資料庫
	Meta        Metaer        // 元資料
	Expire      time.Duration // 過期時間, 若為0表示不過期
	Key         string        // 索引值
	Data        *T            // 資料物件
	// contains filtered or unexported fields
}

SetNX 設值行為, 當索引不存在才執行, 以索引字串與資料到主要/次要資料庫中儲存資料, 使用上有以下幾點須注意

  • 需要事先建立好資料結構, 並填寫到泛型類型T中, 請不要填入指標類型
  • 資料結構如果包含 Save 結構或是符合 Saver 介面, 會套用儲存判斷機制, 減少不必要的儲存操作
  • 資料結構的成員都需要設定好`bson:xxxxx`屬性
  • 執行前設定好 MajorEnable, MinorEnable
  • 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
  • 執行前設定好 Expire, 若不設置或是設置為0表示不過期
  • 執行前設定好 Key 並且不能為空字串
  • 執行前設定好 Data 並且不能為nil

func (*SetNX[T]) Complete

func (this *SetNX[T]) Complete() error

Complete 完成處理

func (*SetNX[T]) Prepare

func (this *SetNX[T]) Prepare() error

Prepare 前置處理

type Submit

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

Submit 混合資料庫執行器, 執行命令時需要遵循以下流程

  • 定義包含了 Behave 的行為結構 / 定義繼承了 Behavior 的行為結構
  • 建立行為資料, 並且填寫內容(例如索引值, 資料內容, 結果成員等)
  • 取得執行物件
  • 新增行為到執行物件中
  • 執行命令 Exec
  • 檢查執行命令結果以及進行後續處理

目前已經實作了幾個預設行為 Lock, Unlock, Get, Set, Index 可以幫助使用者作行為設計; 其中 Lock, Unlock 已經直接整合到 Submit 提供的函式

func (*Submit) Add

func (this *Submit) Add(behavior ...Behavior) *Submit

Add 新增行為

func (*Submit) Exec

func (this *Submit) Exec() error

Exec 執行命令, 執行命令時, 會以下列順序執行

  • 執行所有行為的 Prepare 函式, 如果有錯誤就中止執行
  • 執行主要資料庫的管線處理
  • 執行所有行為的 Complete 函式, 如果有錯誤就中止執行
  • 執行次要資料庫的管線處理
  • 清除管線資料

func (*Submit) Lock

func (this *Submit) Lock(key string) *Submit

Lock 新增鎖定行為

func (*Submit) LockIf

func (this *Submit) LockIf(key string, lock bool) *Submit

LockIf 新增鎖定行為, 依靠旗標來判斷是否要鎖定

func (*Submit) Unlock

func (this *Submit) Unlock(key string) *Submit

Unlock 新增解鎖行為

func (*Submit) UnlockIf

func (this *Submit) UnlockIf(key string, unlock bool) *Submit

UnlockIf 新增解鎖行為, 依靠旗標來判斷是否要解鎖

type Unlock

type Unlock struct {
	Behave        // 行為物件
	Key    string // 索引值
	// contains filtered or unexported fields
}

Unlock 解鎖行為, 解除被 Lock 行為鎖定的索引, 使用上有以下幾點須注意

  • 執行前設定好 Key 並且不能為空字串

func (*Unlock) Complete

func (this *Unlock) Complete() error

Complete 完成處理

func (*Unlock) Prepare

func (this *Unlock) Prepare() error

Prepare 前置處理

Jump to

Keyboard shortcuts

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