1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-25 20:40:49 +00:00

Update: session README

This commit is contained in:
Dobrosław Żybort 2013-06-20 14:22:06 +02:00
parent b68a37a115
commit cc6c84b317

View File

@ -1,60 +1,60 @@
sessionmanager
session
==============
sessionmanager is a golang session manager. It can use session for many providers.Just like the `database/sql` and `database/sql/driver`.
session is a Go session manager. It can use many session providers. Just like the `database/sql` and `database/sql/driver`.
##How to install
## How to install?
go get github.com/astaxie/beego/session
##how many providers support
Now this sessionmanager support memory/file/redis/mysql
## What providers are supported?
As of now this session manager support memory, file, Redis and MySQL.
## How to use it?
##How do we use it?
first you must import it
First you must import it
import (
"github.com/astaxie/beego/session"
)
then in you web app init the globalsession manager
Then in you web app init the global session manager
var globalSessions *session.Manager
use memory as providers:
* Use **memory** as provider:
func init() {
globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"")
go globalSessions.GC()
}
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](https://github.com/Go-SQL-Driver/MySQL#dsn-data-source-name):
* Use **MySQL** as provider, the last param is the DNS, learn more from [mysql](https://github.com/Go-SQL-Driver/MySQL#dsn-data-source-name):
func init() {
globalSessions, _ = session.NewManager("mysql", "gosessionid", 3600,"username:password@protocol(address)/dbname?param=value")
go globalSessions.GC()
}
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:
* Use **file** as provider, the last param is the path where you want file to be stored:
func init() {
globalSessions, _ = session.NewManager("file", "gosessionid", 3600,"./tmp")
go globalSessions.GC()
}
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:
* Use **Redis** as provider, the last param is the Redis conn address:
func init() {
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600,"127.0.0.1:6379")
go globalSessions.GC()
}
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
Finally in the handlerfunc you can use it like this
func login(w http.ResponseWriter, r *http.Request) {
sess := globalSessions.SessionStart(w, r)
@ -70,19 +70,21 @@ at last in the handlerfunc you can use it like this
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.
## How to write own provider?
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.
When you develop a web app, maybe you want to write own provider because you must meet the requirements.
Writing a provider is easy. You only need to define two struct types
(Session and Provider), which satisfy the interface definition.
Maybe you will find the **memory** provider as good example.
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
Set(key, value interface{}) error // set session value
Get(key interface{}) interface{} // get session value
Delete(key interface{}) error // delete session value
SessionID() string // return current sessionID
SessionRelease() // release the resource
}
@ -93,6 +95,7 @@ Write a provider is so easy. You only define two struct type(Session and Provide
SessionGC()
}
##LICENSE
BSD License http://creativecommons.org/licenses/BSD/
## LICENSE
BSD License http://creativecommons.org/licenses/BSD/