sqlitestore

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2022 License: MIT Imports: 10 Imported by: 0

README

sessionup-sqlitestore

GoDoc Test coverage Go Report Card

This is an SQLite session store implementation for sessionup

Installation

To install simply use:

go get github.com/davseby/sessionup-sqlitestore

Usage

To create and use new SQLiteStore use New method. A working sqlite3 driver is required along with the table name that should be used to store sessions.

db, err := sql.Open("sqlite3", path)
if err != nil {
      // handle error
}

store, err := sqlitestore.New(db, "sessions")
if err != nil {
      // handle error
}

manager := sessionup.NewManager(store)

Expired sessions should be periodically deleted as manager will no longer use them. It can be done by either using DeleteByID or DeleteByUserKey methods or by using Cleanup. Cleanup is a blocking method that periodically deletes all expired sessions. It accepts context and interval parameters. Context is used to close the procedure and interval describes how often the action should be performed.


var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())

wg.Add(1)
go func() {
	// we can use for loop, in case cleanup returns an error, we can
	// restart the process after handling the error.
	for {
		defer wg.Done()

		err := store.Cleanup(ctx, 15 * time.Minute)
		if errors.Is(err, ctx.Err()) {
			return
		}

		// handle error
	}
}()

// to gracefully close cleanup
cancel()
wg.Wait()

Cleanup can also be started when creating SQLiteStore using NewWithCleanup. Additionally it returns error channel and close/cancel delegate function.


db, err := sql.Open("sqlite3", path)
if err != nil {
      // handle error
}

store, errCh, cancel, err := sqlitestore.NewWithCleanup(db, "sessions", 15 * time.Minute)
if err != nil {
      // handle error
}

var wg sync.WaitGroup
wg.Add(1)
go func() {
	defer wg.Done()
	for err := range errCh {
		// log cleanup errors
	}
}()

manager := sessionup.NewManager(store)

// graceful application close
cancel()
wg.Wait() 

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidTable is returned when invalid table name is provided.
	ErrInvalidTable = errors.New("invalid table name")

	// ErrInvalidInterval is returned when invalid cleanup interval is provided.
	ErrInvalidInterval = errors.New("invalid cleanup interval")
)

Functions

This section is empty.

Types

type SQLiteStore

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

SQLiteStore is a SQLite implementation of sessionup.Store.

func New

func New(db *sql.DB, table string) (*SQLiteStore, error)

New creates and returns a fresh intance of SQLiteStore.

func NewWithCleanup added in v1.1.0

func NewWithCleanup(
	db *sql.DB,
	table string,
	dur time.Duration,
) (*SQLiteStore, <-chan error, func(), error)

NewWithCleanup creates and returns a fresh instance of SQLiteStore and additionally spins up cleanup go routine. To manage new go routine a cleanup errors channel and cleanup close delegate function is returned.

func (*SQLiteStore) Cleanup added in v1.1.0

func (st *SQLiteStore) Cleanup(ctx context.Context, dur time.Duration) error

Cleanup periodically removes all expired records from the store by their expiration time. Duration specifies how often cleanup should be ran. If duration is less than zero, an ErrInvalidInterval error will be returned.

func (*SQLiteStore) Create

func (st *SQLiteStore) Create(ctx context.Context, session sessionup.Session) error

Create inserts provided session into the store.

func (*SQLiteStore) DeleteByID

func (st *SQLiteStore) DeleteByID(ctx context.Context, id string) error

DeleteByID deletes the session from the store by the provided ID. If session is found OnDeletion handlers will be executed. If session is not found, this function will be no-op.

func (*SQLiteStore) DeleteByUserKey

func (st *SQLiteStore) DeleteByUserKey(ctx context.Context, key string, expIDs ...string) error

DeleteByUserKey deletes all sessions associated with the provided user key, except those whose IDs are provided as last argument. For each deleted session all OnDeletion handlers will be executed. If none are found, this function will no-op.

func (*SQLiteStore) FetchByID

func (st *SQLiteStore) FetchByID(ctx context.Context, id string) (sessionup.Session, bool, error)

FetchByID retrieves a session from the store by the provided ID. The second returned value indicates whether the session was found or not (true == found), error will be nil if session is not found. If session is expired, it will be treated as not found.

func (*SQLiteStore) FetchByUserKey

func (st *SQLiteStore) FetchByUserKey(ctx context.Context, key string) ([]sessionup.Session, error)

FetchByUserKey retrieves all sessions associated with the provided user key. If none are found, both return values will be nil. If list contains expired sessions, they will not be returned.

func (*SQLiteStore) OnDeletion added in v1.1.0

func (st *SQLiteStore) OnDeletion(fn func(context.Context, sessionup.Session)) func()

OnDeletion sets provided handler to be executed whenever a session is deleted. Unsubscribe method is returned, that allows to unset the handler.

Jump to

Keyboard shortcuts

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