ScyllaDb
使用 gocql 的一个适用于 Fiber 的 ScyllaDb 存储引擎。
目录
签名
func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, value []byte, expire time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *gocql.Session
安装
ScyllaDb 在支持模块的最后 2 个 Go 版本上进行了测试。所以如果你还没有初始化模块,请务必先进行初始化
go mod init github.com/<user>/<repo>
然后安装 scylladb 实现
go get github.com/gofiber/storage/scylladb
示例
导入存储包。
import "github.com/gofiber/storage/scylladb"
你可以使用以下几种方式来创建一个存储
// Initialize default config
store := scylladb.New()
// Initialize custom config
store := scylladb.New(scylladb.Config{
Keyspace: "fiber",
Hosts: []string{"127.0.0.1"},
Port: 9042,
Table: "fiber_storage",
Consistency: "ONE",
Reset: false,
})
// Initialize with support for TLS (SslOptions configures TLS use)
//
// InsecureSkipVerify and EnableHostVerification interact as follows:
//
// |Config.InsecureSkipVerify | EnableHostVerification | Result |
// |--------------------------|------------------------|--------------------|
// |Config is nil | false | do not verify host |
// |Config is nil | true | verify host |
// |false | false | verify host |
// |true | false | do not verify host |
// |false | true | verify host |
// |true | true | verify host |
store := New(
Config{
Keyspace: "fiber",
Hosts: []string{"127.0.0.1"},
Port: 9042,
Table: "fiber_storage",
Consistency: "ONE",
SslOpts: &gocql.SslOptions{
Config: &tls.Config{
InsecureSkipVerify: false, // Set this too false to enable certificate verification
},
CertPath: "/path/to/client_cert.pem", // Path to the client certificate
KeyPath: "/path/to/client_key.pem", // Path to the client certificate's private key
CaPath: "/path/to/ca_cert.pem", // Path to the CA certificate
EnableHostVerification: true, // Enable hostname verification
},
Reset: false,
},
)
// Initialize custom config using scylladb connection
cluster, _ := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "fiber"
cluster.Port = 9042
session, _ := cluster.CreateSession()
store := scylladb.New(scylladb.Config{
Session: session,
Keyspace: "fiber",
Table: "fiber_storage",
Reset: false,
})
配置
type Config struct {
// Session is provided by the user to use an existing ScyllaDb session
// Session Will override Keyspace and all other authentication values if used
//
// Optional. Default is nil
Session *gocql.Session
// Keyspace name
//
// Optional. Default is "fiber"
Keyspace string
// Hosts are an array of network addresses for establishing initial connections
// You have the flexibility to specify one or multiple addresses as needed
//
// Optional. Default is "127.0.0.1"
Hosts []string
// Port where the ScyllaDb cluster is listening on
//
// Optional. Default is 9042
Port int
// Username for ScyllaDb cluster
//
// Optional. Default is ""
Username string
// Password for ScyllaDb cluster
//
// Optional. Default is ""
Password string
// Table name
//
// Optional. Default is "fiber_storage"
Table string
// Level of the consistency
//
// Optional. Default is "LOCAL_ONE"
Consistency string
// SslOpts configures TLS use.
//
// Optional. Default is nil
SslOpts *gocql.SslOptions
// Reset clears any existing keys in existing Table
//
// Optional. Default is false
Reset bool
}
默认配置
// ConfigDefault is the default config
var ConfigDefault = Config{
Session: nil,
Keyspace: "fiber",
Hosts: []string{"127.0.0.1"},
Username: "",
Password: "",
Port: 9042,
Table: "fiber_storage",
Consistency: "LOCAL_ONE",
SslOpts: nil,
Reset: false,
}