Etcd
一个使用 etcd-io/etcd
的 Etcd 存储驱动。
注意:需要 Go 1.19 及以上版本
目录
签名
func New(config ...Config) *Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *clientv3.Client
安装
Etcd 在支持模块的 Go 最后两个版本上进行了测试。所以如果你还没有初始化 Go 模块,请务必先初始化一个。
go mod init github.com/<user>/<repo>
然后安装 etcd 实现
go get github.com/gofiber/storage/etcd/v2
示例
导入 storage 包。
import "github.com/gofiber/storage/etcd/v2"
你可以使用以下方式创建存储
// Initialize default config
store := etcd.New()
// Initialize custom config
store := etcd.New(Config{
Endpoints: []string{"localhost:2379"},
})
配置
type Config struct {
// Endpoints is a list of URLs.
Endpoints []string
// DialTimeout is the timeout for failing to establish a connection.
DialTimeout time.Duration
// Username is a username for authentication.
Username string
// Password is a password for authentication.
Password string
// TLS holds the client secure credentials, if any.
TLS *tls.Config
}
默认配置
var ConfigDefault = Config{
Endpoints: []string{"localhost:2379"},
DialTimeout: 2 * time.Second,
Username: "",
Password: "",
TLS: nil,
}