1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 18:30:54 +00:00
Beego/session
yecrane 67695fac9e Update sess_redis.go
修改redis重启后不会自动连接
2013-05-22 22:26:26 +08:00
..
README.md add session's readme 2013-04-08 13:02:45 +08:00
sess_file.go fix session file write bug 2013-05-14 07:33:00 +08:00
sess_gob.go session 2013-04-05 23:50:53 +08:00
sess_mem.go session 2013-04-05 23:50:53 +08:00
sess_mysql.go update session's key from string to interface 2013-05-13 23:30:59 +08:00
sess_redis.go Update sess_redis.go 2013-05-22 22:26:26 +08:00
sess_test.go add session test & delete readme 2013-05-08 22:31:36 +08:00
session.go update session fix 2013-05-08 11:07:55 +08:00

sessionmanager

sessionmanager is a golang session manager. It can use session for many providers.Just like the database/sql and database/sql/driver.

##How to install

go get github.com/astaxie/beego/session

##how many providers support Now this sessionmanager support memory/file/redis/mysql

##How do we use it?

first you must import it

import (
	"github.com/astaxie/beego/session"
)

then in you web app init the globalsession manager

var globalSessions *session.Manager

use memory as providers:

func init() {
	globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"")
	go globalSessions.GC()
}

use mysql as providers,the last param is the DNS, learn more from mysql:

func init() {
	globalSessions, _ = session.NewManager("mysql", "gosessionid", 3600,"username:password@protocol(address)/dbname?param=value")
	go globalSessions.GC()
}

use file as providers,the last param is the path where to store the file:

func init() {
	globalSessions, _ = session.NewManager("file", "gosessionid", 3600,"./tmp")
	go globalSessions.GC()
}

use redis as providers,the last param is the redis's conn address:

func init() {
	globalSessions, _ = session.NewManager("redis", "gosessionid", 3600,"127.0.0.1:6379")
	go globalSessions.GC()
}

at last in the handlerfunc you can use it like this

func login(w http.ResponseWriter, r *http.Request) {
	sess := globalSessions.SessionStart(w, r)
	defer sess.SessionRelease()
	username := sess.Get("username")
	fmt.Println(username)
	if r.Method == "GET" {
		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, nil)
	} else {
		fmt.Println("username:", r.Form["username"])
		sess.Set("username", r.Form["username"])
		fmt.Println("password:", r.Form["password"])
	}
}

##How to write own provider When we develop a web app, maybe you want to write a provider because you must meet the requirements.

Write a provider is so easy. You only define two struct type(Session and Provider),which satisfy the interface definition.Maybe The memory provider is a good example for you.

type SessionStore interface {
	Set(key, value interface{}) error //set session value
	Get(key interface{}) interface{}  //get session value
	Delete(key interface{}) error     //delete session value
	SessionID() string                //back current sessionID
	SessionRelease()                  // release the resource
}

type Provider interface {
	SessionInit(maxlifetime int64, savePath string) error
	SessionRead(sid string) (SessionStore, error)
	SessionDestroy(sid string) error
	SessionGC()
}

##LICENSE

BSD License http://creativecommons.org/licenses/BSD/