Add more term fixes and markdown format fixes
This commit is contained in:
@@ -32,8 +32,8 @@ session 的基本原理是由伺服器為每個會話維護一份資訊資料,
|
||||
### Session 管理器
|
||||
|
||||
定義一個全域性的 session 管理器
|
||||
```Go
|
||||
|
||||
```Go
|
||||
type Manager struct {
|
||||
cookieName string // private cookiename
|
||||
lock sync.Mutex // protects session
|
||||
@@ -48,11 +48,11 @@ func NewManager(provideName, cookieName string, maxLifeTime int64) (*Manager, er
|
||||
}
|
||||
return &Manager{provider: provider, cookieName: cookieName, maxLifeTime: maxLifeTime}, nil
|
||||
}
|
||||
|
||||
```
|
||||
Go 實現整個的流程應該也是這樣的,在 main 套件中建立一個全域性的 session 管理器
|
||||
```Go
|
||||
|
||||
Go 實現整個的流程應該也是這樣的,在 main 套件中建立一個全域性的 session 管理器
|
||||
|
||||
```Go
|
||||
var globalSessions *session.Manager
|
||||
//然後在 init 函式中初始化
|
||||
func init() {
|
||||
@@ -60,8 +60,8 @@ func init() {
|
||||
}
|
||||
```
|
||||
我們知道 session 是儲存在伺服器端的資料,它可以以任何的方式儲存,比如儲存在記憶體、資料庫或者檔案中。因此我們抽象出一個 Provider 介面,用以表徵 session 管理器底層儲存結構。
|
||||
```Go
|
||||
|
||||
```Go
|
||||
type Provider interface {
|
||||
SessionInit(sid string) (Session, error)
|
||||
SessionRead(sid string) (Session, error)
|
||||
@@ -75,8 +75,8 @@ type Provider interface {
|
||||
- SessionGC 根據 maxLifeTime 來刪除過期的資料
|
||||
|
||||
那麼 Session 介面需要實現什麼樣的功能呢?有過 Web 開發經驗的讀者知道,對 Session 的處理基本就 設定值、讀取值、刪除值以及取得當前 sessionID 這四個操作,所以我們的 Session 介面也就實現這四個操作。
|
||||
```Go
|
||||
|
||||
```Go
|
||||
type Session interface {
|
||||
Set(key, value interface{}) error // set session value
|
||||
Get(key interface{}) interface{} // get session value
|
||||
@@ -87,7 +87,6 @@ type Session interface {
|
||||
>以上設計思路來源於 database/sql/driver,先定義好介面,然後具體的儲存 session 的結構實現相應的介面並註冊後,相應功能這樣就可以使用了,以下是用來隨需註冊儲存 session 的結構的 Register 函式的實現。
|
||||
|
||||
```Go
|
||||
|
||||
var provides = make(map[string]Provider)
|
||||
|
||||
// Register makes a session provide available by the provided name.
|
||||
@@ -108,7 +107,6 @@ func Register(name string, provider Provider) {
|
||||
Session ID 是用來識別訪問 Web 應用的每一個使用者,因此必須保證它是全域性唯一的(GUID),下面程式碼展示了如何滿足這一需求:
|
||||
|
||||
```Go
|
||||
|
||||
func (manager *Manager) sessionId() string {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
@@ -119,8 +117,8 @@ func (manager *Manager) sessionId() string {
|
||||
```
|
||||
### session 建立
|
||||
我們需要為每個來訪使用者分配或取得與他相關連的 Session,以便後面根據 Session 資訊來驗證操作。SessionStart 這個函式就是用來檢測是否已經有某個 Session 與當前來訪使用者發生了關聯,如果沒有則建立之。
|
||||
```Go
|
||||
|
||||
```Go
|
||||
func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session Session) {
|
||||
manager.lock.Lock()
|
||||
defer manager.lock.Unlock()
|
||||
@@ -138,8 +136,8 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
|
||||
}
|
||||
```
|
||||
我們用前面 login 操作來示範 session 的運用:
|
||||
```Go
|
||||
|
||||
```Go
|
||||
func login(w http.ResponseWriter, r *http.Request) {
|
||||
sess := globalSessions.SessionStart(w, r)
|
||||
r.ParseForm()
|
||||
@@ -157,8 +155,8 @@ func login(w http.ResponseWriter, r *http.Request) {
|
||||
SessionStart 函式回傳的是一個滿足 Session 介面的變數,那麼我們該如何用他來對 session 資料進行操作呢?
|
||||
|
||||
上面的例子中的程式碼`session.Get("uid")`已經展示了基本的讀取資料的操作,現在我們再來看一下詳細的操作:
|
||||
```Go
|
||||
|
||||
```Go
|
||||
func count(w http.ResponseWriter, r *http.Request) {
|
||||
sess := globalSessions.SessionStart(w, r)
|
||||
createtime := sess.Get("createtime")
|
||||
@@ -185,8 +183,8 @@ func count(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
### session 重置
|
||||
我們知道,Web 應用中有使用者退出這個操作,那麼當用戶退出應用的時候,我們需要對該使用者的 session 資料進行刪除操作,上面的程式碼已經示範了如何使用 session 重置操作,下面這個函式就是實現了這個功能:
|
||||
```Go
|
||||
|
||||
```Go
|
||||
//Destroy sessionid
|
||||
func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request){
|
||||
cookie, err := r.Cookie(manager.cookieName)
|
||||
@@ -201,19 +199,18 @@ func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request){
|
||||
http.SetCookie(w, &cookie)
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### session 刪除
|
||||
我們來看一下 Session 管理器如何來管理刪除,只要我們在 Main 啟動的時候啟動:
|
||||
```Go
|
||||
|
||||
```Go
|
||||
func init() {
|
||||
go globalSessions.GC()
|
||||
}
|
||||
```
|
||||
|
||||
```Go
|
||||
|
||||
func (manager *Manager) GC() {
|
||||
manager.lock.Lock()
|
||||
defer manager.lock.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user