top

Package mysqlstore is a MySQL-based storage engine for the SCS session package.

A working MySQL database is required, containing a sessions table with the definition:

CREATE TABLE sessions (
  token CHAR(43) PRIMARY KEY,
  data BLOB NOT NULL,
  expiry TIMESTAMP(6) NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions (expiry);

The mysqlstore package provides a background 'cleanup' goroutine to delete expired session data. This stops the database table from holding on to invalid sessions forever and growing unnecessarily large.

Usage:

func main() {
    // Establish a database/sql pool
    db, err := sql.Open("mysql", "user:pass@/db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // Create a new MySQLStore instance using the existing database/sql pool,
    // with a cleanup interval of 5 minutes.
    engine := mysqlstore.New(db, 5*time.Minute)

    sessionManager := session.Manage(engine)
    http.ListenAndServe(":4000", sessionManager(http.DefaultServeMux))
}

It is underpinned by the go-sql-driver/mysql driver (https://github.com/go-sql-driver/mysql).

Imported only in test by 1 package(s)

  1. github.com/alexedwards/scs

Imports 1 package(s)

  1. github.com/go-sql-driver/mysql