beego mysql session_beego——session模块

session介绍

session是一个独立的模块,即你可以那这个模块应用于其它Go程序中。

session模块是用来存储客户端用户,session目前只支持cookie方式的请求,如果客户端不支持cookie,那么就无法使用该模块。

session模块参考了database/sql的引擎写法,采用了一个接口,多个实现的方式。

目前实现了memory、file、Redis和MySQL四种存储引擎。

通过下面的方式安装session:

go get github.com/astaxie/beego/session

session使用

首先你必须导入包:

import (

"github.com/astaxie/beego/session"

)

然后你初始化一个全局变量用来存储session控制器:

var globalSessions *session.Manager

接着在你的入口函数中初始化数据:

func init() {

sessionConfig := &session.ManagerConfig{

CookieName:"gosessionid",

EnableSetCookie: true,

Gclifetime:3600,

Maxlifetime: 3600,

Secure: false,

CookieLifeTime: 3600,

ProviderConfig: "./tmp",

}

globalSessions, _ = session.NewManager("memory",sessionConfig)

go globalSessions.GC()

}

NewManager函数的参数的函数如下所示:

(1)引擎名字,可以是 memory、file、mysql 或 redis。

(2)一个 JSON 字符串,传入 Manager 的配置信息

cookieName:客户端存储 cookie 的名字。

enableSetCookie,omitempty: 是否开启 SetCookie,omitempty 这个设置

gclifetime:触发 GC 的时间。

maxLifetime:服务器端存储的数据的过期时间

secure:是否开启 HTTPS,在 cookie 设置的时候有 cookie.Secure 设置。

sessionIDHashFunc:sessionID 生产的函数,默认是 sha1 算法。

sessionIDHashKey: hash 算法中的 key。

cookieLifeTime:客户端存储的 cookie 的时间,默认值是 0,即浏览器生命周期。

providerConfig: 配置信息,根据不同的引擎设置不同的配置信息,详细的配置请看下面的引擎设置

最后我们的业务逻辑处理函数中可以这样调用:

func login(w http.ResponseWriter, r *http.Request) {

sess, _ := globalSessions.SessionStart(w, r)

defer sess.SessionRelease(w)

username := sess.Get("username")

if r.Method == "GET" {

t, _ := template.ParseFiles("login.gtpl")

t.Execute(w, nil)

} else {

sess.Set("username", r.Form["username"])

}

}

globalSessions 有多个函数如下所示:

SessionStart 根据当前请求返回 session 对象

SessionDestroy 销毁当前 session 对象

SessionRegenerateId 重新生成 sessionID

GetActiveSession 获取当前活跃的 session 用户

SetHashFunc 设置 sessionID 生成的函数

SetSecure 设置是否开启 cookie 的 Secure 设置

返回的 session 对象是一个 Interface,包含下面的方法

Set(key, value interface{}) error

Get(key interface{}) interface{}

Delete(key interface{}) error

SessionID() string

SessionRelease()

Flush() error

引擎设置

上面已经展示了 memory 的设置,接下来我们看一下其他三种引擎的设置方式:

(1)mysql

其他参数一样,只是第四个参数配置设置如下所示:

username:password@protocol(address)/dbname?param=value

(2)redis

配置文件信息如下所示,表示链接的地址,连接池,访问密码,没有保持为空:

注意:若使用redis等引擎作为session backend,请在使用前导入 < _ “github.com/astaxie/beego/session/redis” >

(3)file

配置文件如下所示,表示需要保存的目录,默认是两级目录新建文件,例如 sessionID 是 xsnkjklkjjkh27hjh78908,那么目录文件应该是 ./tmp/x/s/xsnkjklkjjkh27hjh78908:

./tmp

如何创建自己的引擎

在开发应用中,你可能需要实现自己的 session 引擎,beego 的这个 session 模块设计的时候就是采用了 interface,所以你可以根据接口实现任意的引擎,例如 memcache 的引擎。

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 & save data to provider

Flush() error //delete all data

}

type Provider interface {

SessionInit(maxlifetime int64, savePath string) error

SessionRead(sid string) (SessionStore, error)

SessionExist(sid string) bool

SessionRegenerate(oldsid, sid string) (SessionStore, error)

SessionDestroy(sid string) error

SessionAll() int //get all active session

SessionGC()

}

最后需要注册自己写的引擎:

func init() {

Register("own", ownadaper)

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值