當前位置: 首頁>>代碼示例>>Golang>>正文


Golang base.Session類代碼示例

本文整理匯總了Golang中github.com/philpearl/tt_goji_middleware/base.Session的典型用法代碼示例。如果您正苦於以下問題:Golang Session類的具體用法?Golang Session怎麽用?Golang Session使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Session類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Get

/*
Get the session for this request from Redis
*/
func (sh *SessionHolder) Get(c web.C, r *http.Request) (*base.Session, error) {
	sessionId := sh.GetSessionId(r)
	if sessionId == "" {
		return nil, base.ErrorSessionNotFound
	}

	conn := c.Env["redis"].(redigo.Conn)

	sess, err := conn.Do("GET", sessionKey(sessionId))
	if err != nil {
		return nil, err
	}
	if sess == nil {
		return nil, base.ErrorSessionNotFound
	}
	sessionBytes, err := redigo.Bytes(sess, err)
	if err != nil {
		return nil, err
	}

	dec := gob.NewDecoder(bytes.NewReader(sessionBytes))
	var session base.Session
	err = dec.Decode(&session)
	if err == nil {
		session.SetId(sessionId)
	}

	return &session, err
}
開發者ID:timisbusy,項目名稱:tt_goji_middleware,代碼行數:32,代碼來源:session.go

示例2: ResetTTL

func (sh *SessionHolder) ResetTTL(c web.C, session *base.Session) error {
	sessionId := session.Id()
	conn := c.Env["redis"].(redigo.Conn)
	_, err := conn.Do("EXPIRE", sessionKey(sessionId), sh.Timeout)

	return err
}
開發者ID:timisbusy,項目名稱:tt_goji_middleware,代碼行數:7,代碼來源:session.go

示例3: Destroy

/*
Destroy deletes a session from redis
*/
func (sh *SessionHolder) Destroy(c web.C, session *base.Session) error {
	sessionId := session.Id()
	delete(c.Env, "session")
	conn := c.Env["redis"].(redigo.Conn)

	_, err := conn.Do("DEL", sessionKey(sessionId))
	return err
}
開發者ID:timisbusy,項目名稱:tt_goji_middleware,代碼行數:11,代碼來源:session.go

示例4: Save

/*
Save a session to postgres
*/
func (sh *SessionHolder) Save(c web.C, session *base.Session) error {
	sessionId := session.Id()

	// There is a potential race here if the insert fails because the session exists, but it is deleted
	// before we can update it.
	_, err := sh.db.Exec("INSERT INTO sessions (id, content, expires) VALUES ($1, $2, now() + $3 * interval '1 second')", sessionId, sessionValues(session.Values), sh.Timeout)
	if err != nil && isAlreadyExists(err, "sessions") {
		_, err = sh.db.Exec("UPDATE sessions SET content=$2, expires=now() + $3 * interval '1 second' WHERE id=$1", sessionId, sessionValues(session.Values), sh.Timeout)
	}
	return err
}
開發者ID:timisbusy,項目名稱:tt_goji_middleware,代碼行數:14,代碼來源:session.go

示例5: RegenerateId

func (sh *SessionHolder) RegenerateId(c web.C, session *base.Session) (string, error) {
	sessionId := session.Id()
	newSessionId := sh.GenerateSessionId()
	conn := c.Env["redis"].(redigo.Conn)
	_, err := conn.Do("RENAME", sessionKey(sessionId), sessionKey(newSessionId))

	if err == nil {
		/* renaming of session key succeeded in backend... update sessionId in our struct */
		session.SetId(newSessionId)
	} else {
		newSessionId = sessionId
	}
	return newSessionId, err
}
開發者ID:timisbusy,項目名稱:tt_goji_middleware,代碼行數:14,代碼來源:session.go


注:本文中的github.com/philpearl/tt_goji_middleware/base.Session類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。