mysql_storage

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 10 Imported by: 6 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

MySQL Storage

一、这是什么

以MySQL为存储引擎的Storage实现,当前仓库为比较底层的存储层实现,你可以与storage-lock结合使用,或者这个项目mysql-locks里专门封装提供了一些MySQL锁相关的更易用友好的API。

二、安装依赖

go get -u github.com/storage-lock/go-mysql-storage

三、API Examples

3.1 从DSN创建MySQLStorage

在Golang的世界中连接数据库最常见的就是DSN,下面的例子演示了如何从一个DSN创建MySQLStorage:

package main

import (
	"context"
	"fmt"
	mysql_storage "github.com/storage-lock/go-mysql-storage"
)

func main() {

	// 使用一个DSN形式的数据库连接字符串创建ConnectionManager
	testDsn := "root:UeGqAm8CxYGldMDLoNNt@tcp(127.0.0.1:3306)/storage_lock_test"
	connectionManager := mysql_storage.NewMysqlConnectionManagerFromDSN(testDsn)

	// 然后从这个ConnectionManager创建MySQL Storage
	options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.2 从连接属性(ip、端口、用户名、密码等等)中创建MySQLStorage

或者你的配置文件中存放的并不是DSN,而是零散的几个连接属性,下面是一个创建MySQLStorage的例子:

package main

import (
	"context"
	"fmt"
	mysql_storage "github.com/storage-lock/go-mysql-storage"
)

func main() {

	// 数据库连接不是DSN的形式,就是一堆零散的属性,则依次设置,可以得到一个连接管理器
	host := "127.0.0.1"
	port := uint(3306)
	username := "root"
	passwd := "UeGqAm8CxYGldMDLoNNt"
	database := "storage_lock_test"
	connectionManager := mysql_storage.NewMysqlConnectionManager(host, port, username, passwd, database)

	// 然后从这个连接管理器创建MySQL Storage
	options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.3 从sql.DB创建MySQLStorage

或者现在你已经有从其它渠道创建的能够连接到MySQL的sql.DB,则也可以从这个*sql.DB创建MySQLStorage:

package main

import (
	"context"
	"database/sql"
	"fmt"
	mysql_storage "github.com/storage-lock/go-mysql-storage"
	storage "github.com/storage-lock/go-storage"
)

func main() {

	// 假设已经在其它地方初始化数据库连接得到了一个*sql.DB
	testDsn := "root:UeGqAm8CxYGldMDLoNNt@tcp(127.0.0.1:3306)/storage_lock_test"
	db, err := sql.Open("mysql", testDsn)
	if err != nil {
		panic(err)
	}

	// 则可以从这个*sql.DB中创建一个MySQL Storage
	connectionManager := storage.NewFixedSqlDBConnectionManager(db)
	options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

Documentation

Index

Constants

View Source
const MysqlConnectionManagerName = "mysql-connection-manager"
View Source
const StorageName = "mysql-storage"

Variables

View Source
var (
	ErrMysqlStorageOptionsConnectionManagerNil = fmt.Errorf("MysqlStorageOptions.ConnectionManager can not nil")
)
View Source
var MysqlOpen = sql.Open

MysqlOpen 默认走 database/sql,测试可替换,避免示例必须连接真实数据库。

Functions

This section is empty.

Types

type MysqlConnectionManager added in v0.0.3

type MysqlConnectionManager struct {

	// 主机的名字
	Host string

	// 主机的端口
	Port uint

	// 用户名
	User string

	// 密码
	Passwd string

	DatabaseName string

	DSN string
	// contains filtered or unexported fields
}

MysqlConnectionManager 创建一个MySQL的连接管理器

func NewMysqlConnectionManager added in v0.0.3

func NewMysqlConnectionManager(host string, port uint, user, passwd, database string) *MysqlConnectionManager

NewMysqlConnectionManager 从连接属性创建数据库连接

func NewMysqlConnectionManagerFromDsn added in v0.0.3

func NewMysqlConnectionManagerFromDsn(dsn string) *MysqlConnectionManager

NewMysqlConnectionManagerFromDsn 从DSN创建MySQL连接管理器

func NewMysqlConnectionManagerFromSqlDb added in v0.0.3

func NewMysqlConnectionManagerFromSqlDb(db *sql.DB) *MysqlConnectionManager

func (*MysqlConnectionManager) GetDSN added in v0.0.3

func (x *MysqlConnectionManager) GetDSN() string

func (*MysqlConnectionManager) Name added in v0.0.3

func (x *MysqlConnectionManager) Name() string

func (*MysqlConnectionManager) Return added in v0.0.3

func (x *MysqlConnectionManager) Return(ctx context.Context, connection *sql.DB) error

func (*MysqlConnectionManager) SetHost added in v0.0.3

func (*MysqlConnectionManager) Shutdown added in v0.0.3

func (x *MysqlConnectionManager) Shutdown(ctx context.Context) error

func (*MysqlConnectionManager) Take added in v0.0.3

func (x *MysqlConnectionManager) Take(ctx context.Context) (*sql.DB, error)

Take 获取到数据库的连接

type MysqlStorage added in v0.0.3

type MysqlStorage struct {
	// mysql的操作基本都是支持sql92语法的
	*sql_based_storage.SqlBasedStorage
	// contains filtered or unexported fields
}

MysqlStorage 基于Mysql的存储

func NewMysqlStorage added in v0.0.3

func NewMysqlStorage(ctx context.Context, options *MysqlStorageOptions) (*MysqlStorage, error)

func (*MysqlStorage) Capabilities added in v0.0.3

func (x *MysqlStorage) Capabilities() []storage.StorageCapability

Capabilities 声明 MySQL 存储支持的能力 MySQL 通过主键唯一约束和 WHERE 条件更新天然支持 CAS, 通过 UNIX_TIMESTAMP(NOW()) 提供数据库服务器时间

func (*MysqlStorage) CreateWithVersion added in v0.0.3

func (x *MysqlStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) (returnError error)

func (*MysqlStorage) GetName added in v0.0.3

func (x *MysqlStorage) GetName() string

func (*MysqlStorage) Watch added in v0.0.3

func (x *MysqlStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)

type MysqlStorageOptions added in v0.0.3

type MysqlStorageOptions struct {

	// 存放锁的表的名字,如果未指定的话则使用默认的表
	TableName string

	// 用于获取数据库连接
	ConnectionManager storage.ConnectionManager[*sql.DB]
}

MysqlStorageOptions 基于MySQL为存储引擎时的选项

func NewMySQLStorageOptions

func NewMySQLStorageOptions() *MysqlStorageOptions

NewMySQLStorageOptions 兼容别名:2023 年 b093d38 把 NewMySQLStorageOptions 改名为 NewMysqlStorageOptions,但已发布的旧版本对外暴露的一直是 NewMySQLStorageOptions, go-mysql-locks/go-mariadb-locks/storage-lock-cli 及 README 全部沿用旧名。补上别名后 新旧两种写法都能编译,避免下游仅因大小写改名而构建失败。

func NewMysqlStorageOptions added in v0.0.3

func NewMysqlStorageOptions() *MysqlStorageOptions

func (*MysqlStorageOptions) Check added in v0.0.3

func (x *MysqlStorageOptions) Check() error

func (*MysqlStorageOptions) SetConnectionManager added in v0.0.3

func (x *MysqlStorageOptions) SetConnectionManager(connManager storage.ConnectionManager[*sql.DB]) *MysqlStorageOptions

func (*MysqlStorageOptions) SetTableName added in v0.0.3

func (x *MysqlStorageOptions) SetTableName(tableName string) *MysqlStorageOptions

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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