Skip to content

Commit 2a9eff4

Browse files
committedDec 3, 2013
update docs
add module’s session & cache introduction add advantage module for monitor & hot reload
1 parent 71e759a commit 2a9eff4

File tree

6 files changed

+165
-12
lines changed

6 files changed

+165
-12
lines changed
 

‎README.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,24 @@ beego 最新的文档
3838
- view设计
3939
- [模板函数](mvc/template.md)
4040
- [错误处理](mvc/errors.md)
41-
- [静态文件处理](mvc/static.md)
42-
43-
5. beego的模块设计
44-
- session模块
45-
- cache模块
46-
- logs模块
47-
- httplib模块
48-
- context模块
49-
- toolbox模块
50-
- config模块
51-
6. 应用部署
41+
- [静态文件处理](mvc/static.md)
42+
5. [beego的模块设计](module/README.md)
43+
- [session模块](module/session.md)
44+
- [cache模块](module/cache.md)
45+
- [logs模块](module/logs.md)
46+
- [httplib模块](module/httplib.md)
47+
- [context模块](module/context.md)
48+
- [toolbox模块](module/toolbox.md)
49+
- [config模块](module/config.md)
50+
6. beego高级编程
51+
- [进程内监控](advantage/monitor.md)
52+
- [热重启](advantage/reload.md)
53+
7. 应用部署
5254
- nginx部署
5355
- supervisor部署
5456
- apache部署
5557
- 独立部署
56-
7. 应用例子
58+
8. 应用例子
5759
- 在线聊天室
5860
- 短域名服务
5961
- todo列表

‎advantage/monitor.md

Whitespace-only changes.

‎advantage/reload.md

Whitespace-only changes.

‎module/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 模块介绍
2+
beego正在逐步的走向乐高模式,也就是把系统逐步的模块化,让一个一个的模块成为乐高的积木,用户可以把这些积木搭建成自己想要的东西,这个就是目前beego的发展方向,beego1.0发布的时候目前包含下面这些模块,这些模块都是我们平常开发过程中非常有用的:
3+
4+
- [session模块](session.md)
5+
- [cache模块](cache.md)
6+
- [logs模块](logs.md)
7+
- [httplib模块](httplib.md)
8+
- [context模块](context.md)
9+
- [toolbox模块](toolbox.md)
10+
- [config模块](config.md)

‎module/cache.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 缓存模块
2+
beego的cache模块是用来做数据缓存的,设计思路来自于`database/sql`,目前支持file、memcache、memory和redis四种引擎,安装方式如下:
3+
4+
go get github.com/astaxie/beego/cache
5+
6+
## 使用入门
7+
首先引入包:
8+
9+
import (
10+
"github.com/astaxie/beego/cache"
11+
)
12+
13+
然后初始化一个全局变量对象:
14+
15+
bm, err := NewCache("memory", `{"interval":60}`)
16+
17+
然后我们就可以使用bm增删改缓存:
18+
19+
bm.Put("astaxie", 1, 10)
20+
bm.Get("astaxie")
21+
bm.IsExist("astaxie")
22+
bm.Delete("astaxie")
23+
24+
## 引擎设置
25+
上面我们展示了memory的设置

‎module/session.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)
Please sign in to comment.