博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
beego——session模块
阅读量:4604 次
发布时间:2019-06-09

本文共 3361 字,大约阅读时间需要 11 分钟。

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)}

  

转载于:https://www.cnblogs.com/yangmingxianshen/p/10127482.html

你可能感兴趣的文章
Spring入门小实例
查看>>
【转】谈谈浏览器的缓存过期时间
查看>>
angularjs ui-grid如何动态设置行高
查看>>
SecureCRT:[1]SecureCRT配色方案
查看>>
The App Life Cycle & The Main Function
查看>>
python编码问题
查看>>
二叉树遍历Java实现
查看>>
20175202 结对编程练习—四则运算(第一周)
查看>>
IOS开发-UIView之动画效果的实现方法(合集)
查看>>
GIT命令
查看>>
yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
javascript 操作cookie
查看>>
在一个应用里,点击一个按钮,跳转到另外一个应用
查看>>
【Xamarin-IOS 开发环境搭建】
查看>>
centos rar使用
查看>>
python小兵之时间模块
查看>>
线条与线条之间连接的样式
查看>>
正则表达式-元字符
查看>>
python基础
查看>>
django建表
查看>>