|
| 1 | +# session介绍 |
| 2 | +session模块是用来存储客户端用户,session模块目前只支持cookie方式的请求,如果客户端不支持cookie,那么就无法使用该模块。 |
| 3 | + |
| 4 | +session模块参考了`database/sql`的引擎写法,采用了一个接口,多个实现的方式,目前实现了memory, file, Redis 和 MySQL四种存储引擎。 |
| 5 | + |
| 6 | +通过下面的方式安装session: |
| 7 | + |
| 8 | + go get github.com/astaxie/beego/session |
| 9 | + |
| 10 | +## session使用 |
| 11 | +首先你必须import包 |
| 12 | + |
| 13 | + import ( |
| 14 | + "github.com/astaxie/beego/session" |
| 15 | + ) |
| 16 | + |
| 17 | +然后你初始化一个全局的变量用来存储session控制器: |
| 18 | + |
| 19 | + var globalSessions *session.Manager |
| 20 | + |
| 21 | +接着在你的入口函数中初始化数据: |
| 22 | + |
| 23 | + func init() { |
| 24 | + globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"",false,"sha1","sessionidkey",3600) |
| 25 | + go globalSessions.GC() |
| 26 | + } |
| 27 | + |
| 28 | +NewManager函数的参数的函数如下所示 |
| 29 | + |
| 30 | +1. 引擎名字,可以是memory、file、mysql、redis |
| 31 | +2. 客户端存储cookie的名字 |
| 32 | +3. 服务器端存储的数据的过期时间,同时也是触发GC的时间 |
| 33 | +4. 配置信息,根据不同的引擎设置不同的配置信息,详细的配置请看下面的引擎设置 |
| 34 | +5. 是否开启https,在cookie设置的时候有cookie.Secure设置 |
| 35 | +6. sessionID生产的函数,默认是sha1算法 |
| 36 | +7. hash算法中的key |
| 37 | +8. 客户端存储的cookie的时间,默认值是0,浏览器生命周期 |
| 38 | + |
| 39 | +最后我们的业务逻辑处理函数中可以这样调用: |
| 40 | + |
| 41 | + func login(w http.ResponseWriter, r *http.Request) { |
| 42 | + sess := globalSessions.SessionStart(w, r) |
| 43 | + defer sess.SessionRelease() |
| 44 | + username := sess.Get("username") |
| 45 | + if r.Method == "GET" { |
| 46 | + t, _ := template.ParseFiles("login.gtpl") |
| 47 | + t.Execute(w, nil) |
| 48 | + } else { |
| 49 | + sess.Set("username", r.Form["username"]) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | +globalSessions有多个函数如下所示: |
| 54 | + |
| 55 | +- SessionStart 根据当前请求返回session对象 |
| 56 | +- SessionDestroy 销毁当前session对象 |
| 57 | +- SessionRegenerateId 重新生成sessionID |
| 58 | +- GetActiveSession 获取当前活跃的session用户 |
| 59 | +- SetHashFunc 设置sessionID生成的函数 |
| 60 | +- SetSecure 设置是否开启cookie的Secure设置 |
| 61 | + |
| 62 | +返回的session对象是一个Interface,包含下面的方法 |
| 63 | + |
| 64 | +* Set(key, value interface{}) error |
| 65 | +* Get(key interface{}) interface{} |
| 66 | +* Delete(key interface{}) error |
| 67 | +* SessionID() string |
| 68 | +* SessionRelease() |
| 69 | +* Flush() error |
| 70 | + |
| 71 | +## 引擎设置 |
| 72 | +上面已经展示了memory的设置,接下来我们看一下其他三种引擎的设置方式: |
| 73 | + |
| 74 | +- mysql |
| 75 | + |
| 76 | + 其他参数一样,只是第四个参数配置设置如下所示,详细的配置请参考[mysql](https://github.com/go-sql-driver/mysql#dsn-data-source-name): |
| 77 | + |
| 78 | + username:password@protocol(address)/dbname?param=value |
| 79 | + |
| 80 | +- redis |
| 81 | + |
| 82 | + 配置文件信息如下所示,表示链接的地址,连接池,访问密码,没有保持为空: |
| 83 | + |
| 84 | + 127.0.0.1:6379,100,astaxie |
| 85 | + |
| 86 | +- file |
| 87 | + |
| 88 | + 配置文件如下所示,表示需要保存的目录,默认是两级目录新建文件,例如sessionID是`xsnkjklkjjkh27hjh78908`,那么目录文件应该是`./tmp/x/s/xsnkjklkjjkh27hjh78908`: |
| 89 | + |
| 90 | + ./tmp |
| 91 | + |
| 92 | +## 如何创建自己的引擎 |
| 93 | +在开发应用中,你可能需要实现自己的session引擎,beego的这个session模块设计的时候就是采用了interface,所以你可以根据接口实现任意的引擎,例如memcache的引擎。 |
| 94 | + |
| 95 | + type SessionStore interface { |
| 96 | + Set(key, value interface{}) error //set session value |
| 97 | + Get(key interface{}) interface{} //get session value |
| 98 | + Delete(key interface{}) error //delete session value |
| 99 | + SessionID() string //back current sessionID |
| 100 | + SessionRelease() // release the resource & save data to provider |
| 101 | + Flush() error //delete all data |
| 102 | + } |
| 103 | + |
| 104 | + type Provider interface { |
| 105 | + SessionInit(maxlifetime int64, savePath string) error |
| 106 | + SessionRead(sid string) (SessionStore, error) |
| 107 | + SessionExist(sid string) bool |
| 108 | + SessionRegenerate(oldsid, sid string) (SessionStore, error) |
| 109 | + SessionDestroy(sid string) error |
| 110 | + SessionAll() int //get all active session |
| 111 | + SessionGC() |
| 112 | + } |
| 113 | + |
| 114 | +## LICENSE |
| 115 | + |
| 116 | +BSD License http://creativecommons.org/licenses/BSD/ |
0 commit comments