Bbolt
一个使用 etcd-io/bbolt 的 Bbolt 存储驱动。Bolt 是一个纯 Go 的键/值存储,灵感来自 Howard Chu 的 LMDB 项目。该项目的目标是为那些不需要完整的数据库服务器(如 Postgres 或 MySQL)的项目提供一个简单、快速且可靠的数据库。
注意: 需要 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() *bbolt.DB
安装
Bbolt 在最新的 2 个支持模块的 Go 版本上进行了测试。所以如果你还没有这样做,请先初始化一个模块
go mod init github.com/<user>/<repo>
然后安装 s3 实现
go get github.com/gofiber/storage/bbolt/v2
示例
导入 storage 包。
import "github.com/gofiber/storage/bbolt/v2"
你可以使用以下方法创建存储
// Initialize default config
store := bbolt.New()
// Initialize custom config
store := bbolt.New(bbolt.Config{
Database: "my_database.db",
Bucket: "my-bucket",
Reset: false,
})
配置
// Config defines the config for storage.
type Config struct {
// Database path
//
// Optional. Default is "fiber.db"
Database string
// Bbolt bucket name
//
// Optional. Default is "fiber_storage"
Bucket string
// Timeout is the amount of time to wait to obtain a file lock.
// Only available on Darwin and Linux.
//
// Optional. Default is 60 * time.Second.
Timeout time.Duration
// Open database in read-only mode.
//
// Optional. Default is false
ReadOnly bool
// Reset clears any existing keys in existing Bucket
//
// Optional. Default is false
Reset bool
}
默认配置
// ConfigDefault is the default config
var ConfigDefault = Config{
Database: "fiber.db",
Bucket: "fiber_storage",
Timeout: 60 * time.Second,
ReadOnly: false,
Reset: false,
}