github.com/aaudis/GoRedisSession/redis/examples

Redis sessions in Go (golang)


Install
go get github.com/aaudis/GoRedisSession/redis/examples

Documentation

Redis Sessions in Go (golang)

Simple session storage in Redis

Install

$ go get github.com/aaudis/GoRedisSession

Documentation

http://godoc.org/github.com/aaudis/GoRedisSession

Simple usage

package main

import (
	"fmt"
	"github.com/aaudis/GoRedisSession"
	"log"
	"net/http"
)

var (
	redis_session *rsess.SessionConnect
)

func main() {
	// Configurable parameters
	rsess.Prefix = "sess:" // session prefix (in Redis)
	rsess.Expire = 1800    // 30 minute session expiration

	// Connecting to Redis and creating storage instance
	temp_sess, err := rsess.New("sid", 0, "127.0.0.1", 6379)
	if err != nil {
		log.Printf("%s", err)
	}

	redis_session = temp_sess // assing to global variable

	http.HandleFunc("/", Root)
	http.HandleFunc("/get", Get)
	http.HandleFunc("/set", Set)
	http.HandleFunc("/des", Des)
	http.ListenAndServe(":8888", nil)
}

func Root(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-Type", "text/html")
	fmt.Fprintf(w, `
        Redis session storage example:<br><br>
        <a href="/set">Store key in session</a><br>
        <a href="/get">Get key value from session</a><br>
        <a href="/des">Destroy session</a>
    `)
}

// Destroy session
func Des(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	s.Destroy(w)
	fmt.Fprintf(w, "Session deleted!")
}

// Set variable to session
func Set(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	s.Set("UserID", "1000")
	fmt.Fprintf(w, "Setting session variable done!")
}

// Get variable from session
func Get(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	fmt.Fprintf(w, "Value %s", s.Get("UserID"))
}