Skip to content

davseby/sessionup-sqlitestore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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() 

About

SQLite session store implementation for sessionup

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages