beego——session模块
session介绍
session是一个独立的模块,即你可以那这个模块应用于其它Go程序中。
session模块是用来存储客户端用户,session目前只支持cookie方式的请求,如果客户端不支持cookie,那么就无法使用该模块。
session模块参考了database/sql的引擎写法,采用了一个接口,多个实现的方式。
目前实现了memory、file、Redis和MySQL四种存储引擎。
通过下面的方式安装session:
1 | go get github.com/astaxie/beego/session |
session使用
首先你必须导入包:
1 2 3 | import ( "github.com/astaxie/beego/session" ) |
然后你初始化一个全局变量用来存储session控制器:
1 | var globalSessions *session.Manager |
接着在你的入口函数中初始化数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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: 配置信息,根据不同的引擎设置不同的配置信息,详细的配置请看下面的引擎设置
最后我们的业务逻辑处理函数中可以这样调用:
1 2 3 4 5 6 7 8 9 10 11 | 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
其他参数一样,只是第四个参数配置设置如下所示:
1 | 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
:
1 | ./tmp |
如何创建自己的引擎
在开发应用中,你可能需要实现自己的 session 引擎,beego 的这个 session 模块设计的时候就是采用了 interface,所以你可以根据接口实现任意的引擎,例如 memcache 的引擎。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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() } |
最后需要注册自己写的引擎:
1 2 3 | func init() { Register( "own" , ownadaper) } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2017-12-16 API的理解和使用——哈希类型的命令
2017-12-16 API的理解和使用——字符串的命令
2017-12-16 API的理解和使用——全局命令