代码拉取完成,页面将自动刷新
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,
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。