mirror of
				https://github.com/gofiber/storage.git
				synced 2025-11-01 04:02:44 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			175 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			175 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package sqlite3
 | |
| 
 | |
| import (
 | |
| 	"database/sql"
 | |
| 	"fmt"
 | |
| 	"time"
 | |
| 
 | |
| 	_ "github.com/mattn/go-sqlite3"
 | |
| )
 | |
| 
 | |
| // Storage interface that is implemented by storage providers
 | |
| type Storage struct {
 | |
| 	db         *sql.DB
 | |
| 	gcInterval time.Duration
 | |
| 	done       chan struct{}
 | |
| 
 | |
| 	sqlSelect string
 | |
| 	sqlInsert string
 | |
| 	sqlDelete string
 | |
| 	sqlReset  string
 | |
| 	sqlGC     string
 | |
| }
 | |
| 
 | |
| var (
 | |
| 	dropQuery = `DROP TABLE IF EXISTS %s;`
 | |
| 	initQuery = []string{
 | |
| 		`CREATE TABLE IF NOT EXISTS %s (
 | |
| 			k  VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
 | |
| 			v  BLOB NOT NULL,
 | |
| 			e  BIGINT NOT NULL DEFAULT '0'
 | |
| 		);`,
 | |
| 		`CREATE INDEX IF NOT EXISTS e ON %s (e);`,
 | |
| 	}
 | |
| )
 | |
| 
 | |
| // New creates a new storage
 | |
| func New(config ...Config) *Storage {
 | |
| 	// Set default config
 | |
| 	cfg := configDefault(config...)
 | |
| 
 | |
| 	// Create db
 | |
| 	db, err := sql.Open("sqlite3", cfg.Database)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	// Set database options
 | |
| 	db.SetMaxOpenConns(cfg.MaxOpenConns)
 | |
| 	db.SetMaxIdleConns(cfg.MaxIdleConns)
 | |
| 	db.SetConnMaxLifetime(cfg.ConnMaxLifetime)
 | |
| 
 | |
| 	// Ping database
 | |
| 	if err := db.Ping(); err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	// Drop table if set to true
 | |
| 	if cfg.Reset {
 | |
| 		if _, err = db.Exec(fmt.Sprintf(dropQuery, cfg.Table)); err != nil {
 | |
| 			_ = db.Close()
 | |
| 			panic(err)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// Init database queries
 | |
| 	for _, query := range initQuery {
 | |
| 		if _, err := db.Exec(fmt.Sprintf(query, cfg.Table)); err != nil {
 | |
| 			_ = db.Close()
 | |
| 			panic(err)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// Create storage
 | |
| 	store := &Storage{
 | |
| 		db:         db,
 | |
| 		gcInterval: cfg.GCInterval,
 | |
| 		done:       make(chan struct{}),
 | |
| 		sqlSelect:  fmt.Sprintf(`SELECT v, e FROM %s WHERE k=?;`, cfg.Table),
 | |
| 		sqlInsert:  fmt.Sprintf("INSERT OR REPLACE INTO %s (k, v, e) VALUES (?,?,?)", cfg.Table),
 | |
| 		sqlDelete:  fmt.Sprintf("DELETE FROM %s WHERE k=?", cfg.Table),
 | |
| 		sqlReset:   fmt.Sprintf("DELETE FROM %s;", cfg.Table),
 | |
| 		sqlGC:      fmt.Sprintf("DELETE FROM %s WHERE e <= ? AND e != 0", cfg.Table),
 | |
| 	}
 | |
| 
 | |
| 	// Start garbage collector
 | |
| 	go store.gcTicker()
 | |
| 
 | |
| 	return store
 | |
| }
 | |
| 
 | |
| // Get value by key
 | |
| func (s *Storage) Get(key string) ([]byte, error) {
 | |
| 	if len(key) <= 0 {
 | |
| 		return nil, nil
 | |
| 	}
 | |
| 	row := s.db.QueryRow(s.sqlSelect, key)
 | |
| 	// Add db response to data
 | |
| 	var (
 | |
| 		data       = []byte{}
 | |
| 		exp  int64 = 0
 | |
| 	)
 | |
| 	if err := row.Scan(&data, &exp); err != nil {
 | |
| 		if err == sql.ErrNoRows {
 | |
| 			return nil, nil
 | |
| 		}
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	// If the expiration time has already passed, then return nil
 | |
| 	if exp != 0 && exp <= time.Now().Unix() {
 | |
| 		return nil, nil
 | |
| 	}
 | |
| 
 | |
| 	return data, nil
 | |
| }
 | |
| 
 | |
| // Set key with value
 | |
| 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 {
 | |
| 		return nil
 | |
| 	}
 | |
| 	var expSeconds int64
 | |
| 	if exp != 0 {
 | |
| 		expSeconds = time.Now().Add(exp).Unix()
 | |
| 	}
 | |
| 	_, err := s.db.Exec(s.sqlInsert, key, val, expSeconds)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| // Delete entry by key
 | |
| func (s *Storage) Delete(key string) error {
 | |
| 	// Ain't Nobody Got Time For That
 | |
| 	if len(key) <= 0 {
 | |
| 		return nil
 | |
| 	}
 | |
| 	_, err := s.db.Exec(s.sqlDelete, key)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| // Reset all entries, including unexpired
 | |
| func (s *Storage) Reset() error {
 | |
| 	_, err := s.db.Exec(s.sqlReset)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| // Close the database
 | |
| func (s *Storage) Close() error {
 | |
| 	s.done <- struct{}{}
 | |
| 	return s.db.Close()
 | |
| }
 | |
| 
 | |
| // gcTicker starts the gc ticker
 | |
| func (s *Storage) gcTicker() {
 | |
| 	ticker := time.NewTicker(s.gcInterval)
 | |
| 	defer ticker.Stop()
 | |
| 	for {
 | |
| 		select {
 | |
| 		case <-s.done:
 | |
| 			return
 | |
| 		case t := <-ticker.C:
 | |
| 			s.gc(t)
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // gc deletes all expired entries
 | |
| func (s *Storage) gc(t time.Time) {
 | |
| 	_, _ = s.db.Exec(s.sqlGC, t.Unix())
 | |
| }
 | |
| 
 | |
| // Return database client
 | |
| func (s *Storage) Conn() *sql.DB {
 | |
| 	return s.db
 | |
| }
 | 
