1 Star 1 Fork 0

湖底观景/GolangTraining

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
session.go 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
GoesToEleven 提交于 2016-04-21 08:45 +08:00 . changes dir structure
package githubexample
import (
"encoding/json"
"net/http"
"google.golang.org/appengine/memcache"
"github.com/nu7hatch/gouuid"
"golang.org/x/net/context"
)
type Session struct {
ID string
State string
Email string
}
func getSession(ctx context.Context, req *http.Request) Session {
cookie, err := req.Cookie("sessionid")
if err != nil || cookie.Value == "" {
sessionID, _ := uuid.NewV4()
cookie = &http.Cookie{
Name: "sessionid",
Value: sessionID.String(),
}
}
item, err := memcache.Get(ctx, cookie.Value)
if err != nil {
item = &memcache.Item{
Key: cookie.Value,
Value: []byte(""),
}
}
var session Session
json.Unmarshal(item.Value, &session)
session.ID = cookie.Value
return session
}
func putSession(ctx context.Context, res http.ResponseWriter, session Session) {
bs, err := json.Marshal(session)
if err != nil {
return
}
memcache.Set(ctx, &memcache.Item{
Key: session.ID,
Value: bs,
})
http.SetCookie(res, &http.Cookie{
Name: "sessionid",
Value: session.ID,
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjianGood/GolangTraining.git
git@gitee.com:zhangjianGood/GolangTraining.git
zhangjianGood
GolangTraining
GolangTraining
afa19f5c43f3

搜索帮助