package
0.1.1
Repository: https://github.com/golangcollege/scs.git
Documentation: pkg.go.dev

# README

redisstore

godoc

Package redisstore is a Redis-based storage engine for the SCS session package.

Warning: The redisstore API is not finalized and may change, possibly significantly. The package is fine to use as-is, but it is strongly recommended that you vendor the package to avoid compatibility problems in the future.

Usage

Installation

Either:

$ go get github.com/alexedwards/scs/engine/redisstore

Or (recommended) use use gvt to vendor the engine/redisstore and session sub-packages:

$ gvt fetch github.com/alexedwards/scs/engine/redisstore
$ gvt fetch github.com/alexedwards/scs/session

Example

The redisstore package uses the popular Redigo Redis client.

You should follow the Redigo instructions to setup a connection pool, and pass the pool to redisstore.New() to establish the session storage engine.

package main

import (
    "io"
    "net/http"

    "github.com/alexedwards/scs/engine/redisstore"
    "github.com/alexedwards/scs/session"
    "github.com/garyburd/redigo/redis"
)

func main() {
    // Establish a Redigo connection pool.
    pool := &redis.Pool{
        MaxIdle: 10,
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp", "localhost:6379")
        },
    }

    // Create a new RedisStore instance using the connection pool.
    engine := redisstore.New(pool)

    sessionManager := session.Manage(engine)
    http.HandleFunc("/put", putHandler)
    http.HandleFunc("/get", getHandler)
    http.ListenAndServe(":4000", sessionManager(http.DefaultServeMux))
}

func putHandler(w http.ResponseWriter, r *http.Request) {
    err := session.PutString(r, "message", "Hello world!")
    if err != nil {
        http.Error(w, err.Error(), 500)
    }
}

func getHandler(w http.ResponseWriter, r *http.Request) {
    msg, err := session.GetString(r, "message")
    if err != nil {
        http.Error(w, err.Error(), 500)
    }
    io.WriteString(w, msg)
}

Cleaning up expired session data

Redis will automatically remove expired session keys.

Notes

Full godoc documentation: https://godoc.org/github.com/alexedwards/scs/engine/redisstore.

# Functions

New returns a new RedisStore instance.

# Variables

Prefix controls the Redis key prefix.

# Structs

RedisStore represents the currently configured session storage engine.