sqlite3

package module
v0.0.0-...-8c2748f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 7 Imported by: 0

README

SQLite

An SQLite3 storage driver for Fiber using jmoiron/sqlx + mattn/go-sqlite3 which also supports namespacing to allow multiple storage spaces to run independently using the same table / connection pool.

This IS NOT directly compatible with the standard storage drivers for Fiber, but only requires minimal code tweaks for it to work with code that uses those. This differs from the standard Fiber versions in that it allows any data type to be entered and retrieved, and allows values to be recalled either as an interface{} or as its original type.

Table of Contents

Signatures

func New(config ...Config) Storage
func (s *Storage) Get(key string) *Result
func (s *Storage) Set(key string, value any, expiry ...time.Duration) error
func (s *Storage) Delete(keys ...string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *sql.DB

Installation

Install the sqlite3 implementation:

go get github.com/paul-norman/go-fiber-storage/sqlite3

Initialisation

Import the storage package:

import "github.com/paul-norman/go-fiber-storage/sqlite3"

You can use the following possibilities to create a store (defaults do not need to be included, just shown for illustrative purposes):

// Initialise default config
store1 := sqlite3.New()

// Initialise custom config
sessions := sqlite3.New(sqlite3.Config{
	Database:        "./general.sqlite3",
	Table:           "general_store",
	Reset:           false,
	GCInterval:      10 * time.Second,
	MaxOpenConns:    100,
	MaxIdleConns:    100,
	ConnMaxLifetime: 1 * time.Second,
	Namespace:       "sessions",
})

// Initialise custom config using existing DB connection
db, _ := sqlx.Open("sqlite3", YourConfigStruct)
names := sqlite3.New(sqlite3.Config{
	DB:        db,
	Namespace: "names",
})

Usage

package main

import (
	"fmt"
	"time"
	"github.com/paul-norman/go-fiber-storage/sqlite3"
)

func main() {
	store := sqlite3.New()
	defer store.Close()

	// Error handling omitted for brevity
	err := store.Set("my_key", "lives forever")
	err  = store.Set("one_second", "lives for 1 second", 1 * time.Second)
	err  = store.Set("complex_type", map[string]int64{ "test": 123 })

	// Fetch the Result struct, check that we found a value and that there wasn't an error
	result := store.Get("my_key")
	if !result.Miss() && result.Err() == nil {
		fmt.Println("Value: " + result.String()) // Convert the interface{} to a string
	}

	// Fetch parsed information (value, error, miss) separately
	str, err, miss := store.Get("one_second").String()
	if !miss && err == nil {
		fmt.Println("Value: " + str) // If we are here the value is a string
	}

	// Sleep for 2 seconds
	time.Sleep(2 * time.Second)

	// This will result in a miss
	str, err, miss = store.Get("one_second").String()
	if miss {
		fmt.Println("The value has gone...") // str and err will be nil
	}

	// Complex types are also simple
	item, err, miss := store.Get("complex_type").Interface()
	if !miss && err == nil {
		fmt.Println(item.(map[string]int64)) // Convert the interface to the desired type
	}

	// Remove the keys - doesn't matter that one has already expired
	err = store.Delete("my_key", "one_second", "complex_type")

	// Or
	err = store.Reset()
}

Config Options

type Config struct {
	// DB Will override ConnectionURI and all other authentication values if used
	//
	// Optional. Default is nil
	DB *sqlx.DB
	
	// 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

	// Namespace to allow different types of information in the same table
	//
	// Optional. Default is ""
	Namespace string

	// 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
}

Default Config

var ConfigDefault = Config{
	Database:        "./fiber.sqlite3",
	Table:           "fiber_storage",
	Reset:           false,
	GCInterval:      10 * time.Second,
	MaxOpenConns:    100,
	MaxIdleConns:    100,
	ConnMaxLifetime: 1 * time.Second,
	Prefix:          "",
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{

	Database:   "./fiber.sqlite3",
	Table:      "fiber_storage",
	Reset:      false,
	GCInterval: 10 * time.Second,
	Namespace:  "",

	MaxOpenConns:    100,
	MaxIdleConns:    100,
	ConnMaxLifetime: 1 * time.Second,
}

ConfigDefault is the default config

Functions

This section is empty.

Types

type Config

type Config struct {
	// DB is an existing connection to be used
	//
	// Optional. Default is nil
	DB *sqlx.DB

	// 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

	// Namespace to allow different types of information in the same table
	//
	// Optional. Default is ""
	Namespace string

	// 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
}

Config defines the config for storage.

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage interface that is implemented by storage providers

func New

func New(config ...Config) *Storage

New creates a new storage

func (*Storage) Close

func (s *Storage) Close() error

Close the database

func (*Storage) Conn

func (s *Storage) Conn() *sqlx.DB

Return database client

func (*Storage) Delete

func (s *Storage) Delete(keys ...string) error

Delete entries by key

func (*Storage) Get

func (s *Storage) Get(key string) *storage.Result

Get value by key

func (*Storage) Reset

func (s *Storage) Reset() error

Reset all entries in the namespace

func (*Storage) Set

func (s *Storage) Set(key string, value any, expiry ...time.Duration) error

Set key with value

type Store

type Store struct {
	Key       string `db:"key"`
	Value     []byte `db:"value"`
	Namespace string `db:"namespace"`
	Expiry    int64  `db:"expiry"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL