SQLite3
一个使用 mattn/go-sqlite3 的 SQLite3 存储驱动程序。
注意:需要 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() *sql.DB
安装
SQLite3 在最后两个支持模块的 Go 版本上进行了测试。因此,如果你还没有这样做,请务必先初始化一个。
go mod init github.com/<user>/<repo>
然后安装 sqlite3 实现
go get github.com/gofiber/storage/sqlite3/v2
示例
导入存储包。
import "github.com/gofiber/storage/sqlite3/v2"
你可以使用以下几种方式来创建一个存储
// Initialize default config
store := sqlite3.New()
// Initialize custom config
store := sqlite3.New(sqlite3.Config{
Database: "./fiber.sqlite3",
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: 1 * time.Second,
})
配置
type Config struct {
// Database name
//
// Optional. Default is "fiber"
Database string
// Table name
//
// Optional. Default is "fiber_storage"
Table 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
// //////////////////////////////////
// Adaptor related config options //
// //////////////////////////////////
// MaxIdleConns sets the maximum number of connections in the idle connection pool.
//
// Optional. Default is 100.
MaxIdleConns int
// MaxOpenConns sets the maximum number of open connections to the database.
//
// Optional. Default is 100.
MaxOpenConns int
// ConnMaxLifetime sets the maximum amount of time a connection may be reused.
//
// Optional. Default is 1 second.
ConnMaxLifetime time.Duration
}
默认配置
var ConfigDefault = Config{
Database: "./fiber.sqlite3",
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: 1 * time.Second,
}