Neo4j
一个使用 neo4j/neo4j-go-driver 的 Neo4j 存储驱动。
注意:需要 Go 语言最新两个版本
目录
签名
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() neo4j.DriverWithContext
安装
Neo4j 在支持模块的最新 2 个 Go 版本上进行了测试。因此,如果您尚未这样做,请确保首先初始化一个。
go mod init github.com/<user>/<repo>
然后安装 neo4j 实现
go get github.com/gofiber/storage/neo4j
示例
导入 storage 包。
import neo4jstore "github.com/gofiber/storage/neo4j"
您可以使用以下方式创建存储
// Initialize default config
store := neo4j.New()
// Initialize custom config
store := neo4j.New(neo4jstore.Config{
DB: driver,
Node: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
})
配置
// Config defines the config for storage.
type Config struct {
// Connection pool
//
// DB neo4j.DriverWithContext object will override connection uri and other connection fields.
//
// Optional. Default is nil.
DB neo4j.DriverWithContext
// Target Server
//
// Optional. Default is "neo4j://localhost"
URI string
// Connection authentication
//
// Auth auth.TokenManager will override Username and Password fields
//
// Optional. Default is nil.
Auth auth.TokenManager
// Connection configurations
//
// Optional. Default is nil
Configurations []func(*config.Config)
// Server username
//
// Optional. Default is ""
Username string
// Server password
//
// Optional. Default is ""
Password string
// Node name
//
// Optional. Default is "fiber_storage"
Node string
// Reset clears any existing keys in existing Table
//
// Optional. Default is false
Reset bool
// Time before deleting expired keys
//
// Optional. Default is 10 * time.Second
GCInterval time.Duration
}
关于身份验证的说明
如果服务器上启用了身份验证,则必须通过以下三种方式之一提供身份验证(前一种方式会覆盖后一种方式)
- 通过连接池,即在
DB
字段中提供的neo4j.DriverWithContext
。 - 通过
Auth
字段:它必须是一个auth.TokenManager
,其值可以是除neo4j.NoAuth()
以外的任何值。 - 通过同时设置
Username
和Password
字段:这将导致此存储驱动程序使用基本身份验证(Basic Auth)。
否则,您的 neo4j 驱动程序将因授权错误而 panic。
相比之下,如果服务器上禁用了身份验证,则无需提供任何身份验证参数。
默认配置
仅用于可选字段
var ConfigDefault = Config{
URI: "neo4j://localhost",
Node: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
}