mirror of
https://github.com/gofiber/storage.git
synced 2025-10-04 00:06:35 +08:00
Some cleanups
This commit is contained in:
@@ -2,7 +2,6 @@ package mssql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -89,7 +88,7 @@ func New(config ...Config) *Storage {
|
|||||||
db.SetMaxIdleConns(cfg.maxIdleConns)
|
db.SetMaxIdleConns(cfg.maxIdleConns)
|
||||||
db.SetConnMaxLifetime(cfg.connMaxLifetime)
|
db.SetConnMaxLifetime(cfg.connMaxLifetime)
|
||||||
|
|
||||||
// Ping database
|
// Ping database to ensure a connection has been made
|
||||||
if err := db.Ping(); err != nil {
|
if err := db.Ping(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -133,23 +132,24 @@ func New(config ...Config) *Storage {
|
|||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|
||||||
var noRows = errors.New("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) {
|
||||||
if len(key) <= 0 {
|
if len(key) <= 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
row := s.db.QueryRow(s.sqlSelect, key)
|
row := s.db.QueryRow(s.sqlSelect, key)
|
||||||
// Add db response to data
|
|
||||||
var (
|
var (
|
||||||
data = []byte{}
|
data = []byte{}
|
||||||
exp int64 = 0
|
exp int64 = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := row.Scan(&data, &exp); err != nil {
|
if err := row.Scan(&data, &exp); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,24 +163,25 @@ func (s *Storage) Get(key string) ([]byte, error) {
|
|||||||
|
|
||||||
// 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 {
|
||||||
// Ain't Nobody Got Time For That
|
|
||||||
if len(key) <= 0 || len(val) <= 0 {
|
if len(key) <= 0 || len(val) <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var expSeconds int64
|
var expSeconds int64
|
||||||
if exp != 0 {
|
if exp != 0 {
|
||||||
expSeconds = time.Now().Add(exp).Unix()
|
expSeconds = time.Now().Add(exp).Unix()
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := s.db.Exec(s.sqlInsert, key, val, expSeconds)
|
_, err := s.db.Exec(s.sqlInsert, key, val, expSeconds)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete entry by key
|
// Delete entry by key
|
||||||
func (s *Storage) Delete(key string) error {
|
func (s *Storage) Delete(key string) error {
|
||||||
// Ain't Nobody Got Time For That
|
|
||||||
if len(key) <= 0 {
|
if len(key) <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := s.db.Exec(s.sqlDelete, key)
|
_, err := s.db.Exec(s.sqlDelete, key)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user