Coder Social home page Coder Social logo

goredissession's Introduction

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"))
}

goredissession's People

Contributors

aaudis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.