mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 16:48:25 +08:00
📦 MySQL: Add driver
This commit is contained in:
101
mysql/mysql.go
101
mysql/mysql.go
@@ -1,22 +1,83 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/gofiber/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Storage interface that is implemented by storage providers
|
// Storage interface that is implemented by storage providers
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
|
db *sql.DB
|
||||||
gcInterval time.Duration
|
gcInterval time.Duration
|
||||||
|
|
||||||
|
sqlSelect string
|
||||||
|
sqlInsert string
|
||||||
|
sqlDelete string
|
||||||
|
sqlClear string
|
||||||
|
sqlGC string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
dropQuery = "DROP TABLE IF EXISTS %s;"
|
||||||
|
initQuery = []string{
|
||||||
|
"CREATE TABLE IF NOT EXISTS %s ( `id` VARCHAR(64) NOT NULL DEFAULT '' , `data` TEXT NOT NULL , `exp` BIGINT NOT NULL DEFAULT '0' , PRIMARY KEY (`id`));",
|
||||||
|
"CREATE INDEX IF NOT EXISTS exp ON %s (exp);",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// New creates a new storage
|
// New creates a new storage
|
||||||
func New(config ...Config) *Storage {
|
func New(config ...Config) *Storage {
|
||||||
// Set default config
|
// Set default config
|
||||||
cfg := configDefault(config...)
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
|
// Create db
|
||||||
|
db, err := sql.Open("mysql", cfg.makeDSN())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.SetMaxOpenConns(cfg.MaxOpenConns)
|
||||||
|
db.SetMaxIdleConns(cfg.MaxIdleConns)
|
||||||
|
db.SetConnMaxLifetime(cfg.ConnMaxLifetime)
|
||||||
|
|
||||||
|
// Ping database to ensure a connection has been made
|
||||||
|
if err := db.Ping(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drop table if set to true
|
||||||
|
if cfg.DropTable {
|
||||||
|
query := fmt.Sprintf(dropQuery, cfg.TableName)
|
||||||
|
if _, err = db.Exec(query); err != nil {
|
||||||
|
_ = db.Close()
|
||||||
|
fmt.Println(query)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init database queries
|
||||||
|
for _, query := range initQuery {
|
||||||
|
query = fmt.Sprintf(query, cfg.TableName)
|
||||||
|
if _, err := db.Exec(query); err != nil {
|
||||||
|
_ = db.Close()
|
||||||
|
fmt.Println(query)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create storage
|
// Create storage
|
||||||
store := &Storage{
|
store := &Storage{
|
||||||
gcInterval: cfg.GCInterval,
|
gcInterval: cfg.GCInterval,
|
||||||
|
db: db,
|
||||||
|
sqlSelect: fmt.Sprintf(`SELECT data, exp FROM %s WHERE id=?;`, cfg.TableName),
|
||||||
|
sqlInsert: fmt.Sprintf("INSERT INTO %s (id, data, exp) VALUES (?,?,?)", cfg.TableName),
|
||||||
|
sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE id=?", cfg.TableName),
|
||||||
|
sqlClear: fmt.Sprintf("DELETE FROM %s;", cfg.TableName),
|
||||||
|
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE exp <= ?", cfg.TableName),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start garbage collector
|
// Start garbage collector
|
||||||
@@ -25,24 +86,54 @@ func New(config ...Config) *Storage {
|
|||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var noRows = "sql: no rows in result set"
|
||||||
|
|
||||||
// Get value by key
|
// Get value by key
|
||||||
func (s *Storage) Get(key string) ([]byte, error) {
|
func (s *Storage) Get(key string) ([]byte, error) {
|
||||||
|
row := s.db.QueryRow(s.sqlSelect, key)
|
||||||
|
|
||||||
|
// Add db response to data
|
||||||
|
|
||||||
|
var (
|
||||||
|
data []byte
|
||||||
|
exp int64
|
||||||
|
)
|
||||||
|
|
||||||
|
if err := row.Scan(&data, &exp); err != nil {
|
||||||
|
if err.Error() != noRows {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the expiration time has already passed, then return nil
|
||||||
|
if exp <= time.Now().Unix() && exp != 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set key with value
|
// Set key with value
|
||||||
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
|
||||||
return nil
|
var expSeconds int64
|
||||||
|
if exp != 0 {
|
||||||
|
expSeconds = time.Now().Add(exp).Unix()
|
||||||
|
}
|
||||||
|
_, err := s.db.Exec(s.sqlInsert, key, utils.UnsafeString(val), expSeconds)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete key by key
|
// Delete key by key
|
||||||
func (s *Storage) Delete(key string) error {
|
func (s *Storage) Delete(key string) error {
|
||||||
return nil
|
_, err := s.db.Exec(s.sqlDelete, key)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear all keys
|
// Clear all keys
|
||||||
func (s *Storage) Clear() error {
|
func (s *Storage) Clear() error {
|
||||||
return nil
|
_, err := s.db.Exec(s.sqlClear)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Garbage collector to delete expired keys
|
// Garbage collector to delete expired keys
|
||||||
@@ -50,6 +141,8 @@ func (s *Storage) gc() {
|
|||||||
tick := time.NewTicker(s.gcInterval)
|
tick := time.NewTicker(s.gcInterval)
|
||||||
for {
|
for {
|
||||||
<-tick.C
|
<-tick.C
|
||||||
// clean entries
|
if _, err := s.db.Exec(s.sqlGC); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user