示例#1
0
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
}
示例#2
0
/*
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
}
示例#3
0
/*
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
}
示例#4
0
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
}
示例#5
0
func (sh *SessionHolder) RegenerateId(c web.C, session *base.Session) (string, error) {
	sessionId := session.Id()
	newSessionId := sh.GenerateSessionId()

	_, err := sh.db.Exec("UPDATE sessions SET id=$2 WHERE id=$1", sessionId, newSessionId)

	if err == nil {
		// This all worked, use the new session Id
		session.SetId(newSessionId)
	} else {
		// Move back to the old session Id.  We hope this is a transitory error...
		newSessionId = sessionId
	}
	return newSessionId, err
}
示例#6
0
/*
Save a session to redis
*/
func (sh *SessionHolder) Save(c web.C, session *base.Session) error {
	sessionId := session.Id()
	conn := c.Env["redis"].(redigo.Conn)

	var b bytes.Buffer
	enc := gob.NewEncoder(&b)

	err := enc.Encode(session)
	if err != nil {
		return err
	}

	_, err = conn.Do("SET", sessionKey(sessionId), b.String(), "EX", sh.Timeout)

	return err
}
示例#7
0
/*
Destroy deletes a session from postgres
*/
func (sh *SessionHolder) Destroy(c web.C, session *base.Session) error {
	sessionId := session.Id()
	delete(c.Env, "session")
	_, err := sh.db.Exec("DELETE FROM sessions WHERE id=$1", sessionId)
	return err
}
示例#8
0
func (sh *SessionHolder) ResetTTL(c web.C, session *base.Session) error {
	// Need to implement a TTL...
	_, err := sh.db.Exec("UPDATE sessions SET expires=now()+$2 * interval '1 second' WHERE id=$1", session.Id(), sh.Timeout)
	return err
}