mirror of
https://github.com/astaxie/beego.git
synced 2025-06-14 11:50:40 +00:00
Add ctx to session API
This commit is contained in:
@ -33,6 +33,7 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -59,7 +60,7 @@ type SessionStore struct {
|
||||
}
|
||||
|
||||
// Set value in redis session
|
||||
func (rs *SessionStore) Set(key, value interface{}) error {
|
||||
func (rs *SessionStore) Set(ctx context.Context, key, value interface{}) error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
rs.values[key] = value
|
||||
@ -67,7 +68,7 @@ func (rs *SessionStore) Set(key, value interface{}) error {
|
||||
}
|
||||
|
||||
// Get value in redis session
|
||||
func (rs *SessionStore) Get(key interface{}) interface{} {
|
||||
func (rs *SessionStore) Get(ctx context.Context, key interface{}) interface{} {
|
||||
rs.lock.RLock()
|
||||
defer rs.lock.RUnlock()
|
||||
if v, ok := rs.values[key]; ok {
|
||||
@ -77,7 +78,7 @@ func (rs *SessionStore) Get(key interface{}) interface{} {
|
||||
}
|
||||
|
||||
// Delete value in redis session
|
||||
func (rs *SessionStore) Delete(key interface{}) error {
|
||||
func (rs *SessionStore) Delete(ctx context.Context, key interface{}) error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
delete(rs.values, key)
|
||||
@ -85,7 +86,7 @@ func (rs *SessionStore) Delete(key interface{}) error {
|
||||
}
|
||||
|
||||
// Flush clear all values in redis session
|
||||
func (rs *SessionStore) Flush() error {
|
||||
func (rs *SessionStore) Flush(context.Context) error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
rs.values = make(map[interface{}]interface{})
|
||||
@ -93,12 +94,12 @@ func (rs *SessionStore) Flush() error {
|
||||
}
|
||||
|
||||
// SessionID get redis session id
|
||||
func (rs *SessionStore) SessionID() string {
|
||||
func (rs *SessionStore) SessionID(context.Context) string {
|
||||
return rs.sid
|
||||
}
|
||||
|
||||
// SessionRelease save session values to redis
|
||||
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
func (rs *SessionStore) SessionRelease(ctx context.Context, w http.ResponseWriter) {
|
||||
b, err := session.EncodeGob(rs.values)
|
||||
if err != nil {
|
||||
return
|
||||
@ -123,7 +124,7 @@ type Provider struct {
|
||||
// SessionInit init redis session
|
||||
// savepath like redis server addr,pool size,password,dbnum,IdleTimeout second
|
||||
// e.g. 127.0.0.1:6379,100,astaxie,0,30
|
||||
func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
func (rp *Provider) SessionInit(ctx context.Context, maxlifetime int64, savePath string) error {
|
||||
rp.maxlifetime = maxlifetime
|
||||
configs := strings.Split(savePath, ",")
|
||||
if len(configs) > 0 {
|
||||
@ -185,7 +186,7 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
}
|
||||
|
||||
// SessionRead read redis session by sid
|
||||
func (rp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
func (rp *Provider) SessionRead(ctx context.Context, sid string) (session.Store, error) {
|
||||
var kv map[interface{}]interface{}
|
||||
|
||||
kvs, err := rp.poollist.Get(sid).Result()
|
||||
@ -205,7 +206,7 @@ func (rp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
}
|
||||
|
||||
// SessionExist check redis session exist by sid
|
||||
func (rp *Provider) SessionExist(sid string) (bool, error) {
|
||||
func (rp *Provider) SessionExist(ctx context.Context, sid string) (bool, error) {
|
||||
c := rp.poollist
|
||||
|
||||
if existed, err := c.Exists(sid).Result(); err != nil || existed == 0 {
|
||||
@ -215,7 +216,7 @@ func (rp *Provider) SessionExist(sid string) (bool, error) {
|
||||
}
|
||||
|
||||
// SessionRegenerate generate new sid for redis session
|
||||
func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
func (rp *Provider) SessionRegenerate(ctx context.Context, oldsid, sid string) (session.Store, error) {
|
||||
c := rp.poollist
|
||||
if existed, _ := c.Exists(oldsid).Result(); existed == 0 {
|
||||
// oldsid doesn't exists, set the new sid directly
|
||||
@ -226,11 +227,11 @@ func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error)
|
||||
c.Rename(oldsid, sid)
|
||||
c.Expire(sid, time.Duration(rp.maxlifetime)*time.Second)
|
||||
}
|
||||
return rp.SessionRead(sid)
|
||||
return rp.SessionRead(context.Background(), sid)
|
||||
}
|
||||
|
||||
// SessionDestroy delete redis session by id
|
||||
func (rp *Provider) SessionDestroy(sid string) error {
|
||||
func (rp *Provider) SessionDestroy(ctx context.Context, sid string) error {
|
||||
c := rp.poollist
|
||||
|
||||
c.Del(sid)
|
||||
@ -238,11 +239,11 @@ func (rp *Provider) SessionDestroy(sid string) error {
|
||||
}
|
||||
|
||||
// SessionGC Impelment method, no used.
|
||||
func (rp *Provider) SessionGC() {
|
||||
func (rp *Provider) SessionGC(context.Context) {
|
||||
}
|
||||
|
||||
// SessionAll return all activeSession
|
||||
func (rp *Provider) SessionAll() int {
|
||||
func (rp *Provider) SessionAll(context.Context) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
|
@ -40,57 +40,57 @@ func TestRedis(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal("session start failed:", err)
|
||||
}
|
||||
defer sess.SessionRelease(w)
|
||||
defer sess.SessionRelease(nil, w)
|
||||
|
||||
// SET AND GET
|
||||
err = sess.Set("username", "astaxie")
|
||||
err = sess.Set(nil, "username", "astaxie")
|
||||
if err != nil {
|
||||
t.Fatal("set username failed:", err)
|
||||
}
|
||||
username := sess.Get("username")
|
||||
username := sess.Get(nil, "username")
|
||||
if username != "astaxie" {
|
||||
t.Fatal("get username failed")
|
||||
}
|
||||
|
||||
// DELETE
|
||||
err = sess.Delete("username")
|
||||
err = sess.Delete(nil, "username")
|
||||
if err != nil {
|
||||
t.Fatal("delete username failed:", err)
|
||||
}
|
||||
username = sess.Get("username")
|
||||
username = sess.Get(nil, "username")
|
||||
if username != nil {
|
||||
t.Fatal("delete username failed")
|
||||
}
|
||||
|
||||
// FLUSH
|
||||
err = sess.Set("username", "astaxie")
|
||||
err = sess.Set(nil, "username", "astaxie")
|
||||
if err != nil {
|
||||
t.Fatal("set failed:", err)
|
||||
}
|
||||
err = sess.Set("password", "1qaz2wsx")
|
||||
err = sess.Set(nil, "password", "1qaz2wsx")
|
||||
if err != nil {
|
||||
t.Fatal("set failed:", err)
|
||||
}
|
||||
username = sess.Get("username")
|
||||
username = sess.Get(nil, "username")
|
||||
if username != "astaxie" {
|
||||
t.Fatal("get username failed")
|
||||
}
|
||||
password := sess.Get("password")
|
||||
password := sess.Get(nil, "password")
|
||||
if password != "1qaz2wsx" {
|
||||
t.Fatal("get password failed")
|
||||
}
|
||||
err = sess.Flush()
|
||||
err = sess.Flush(nil)
|
||||
if err != nil {
|
||||
t.Fatal("flush failed:", err)
|
||||
}
|
||||
username = sess.Get("username")
|
||||
username = sess.Get(nil, "username")
|
||||
if username != nil {
|
||||
t.Fatal("flush failed")
|
||||
}
|
||||
password = sess.Get("password")
|
||||
password = sess.Get(nil, "password")
|
||||
if password != nil {
|
||||
t.Fatal("flush failed")
|
||||
}
|
||||
|
||||
sess.SessionRelease(w)
|
||||
sess.SessionRelease(nil, w)
|
||||
}
|
||||
|
Reference in New Issue
Block a user