1
0
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:
Ming Deng
2020-08-30 15:39:07 +00:00
parent 0019e0fc1b
commit 670064686e
23 changed files with 302 additions and 288 deletions

View File

@ -33,6 +33,7 @@
package redis_sentinel
import (
"context"
"net/http"
"strconv"
"strings"
@ -58,7 +59,7 @@ type SessionStore struct {
}
// Set value in redis_sentinel 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
@ -66,7 +67,7 @@ func (rs *SessionStore) Set(key, value interface{}) error {
}
// Get value in redis_sentinel 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 {
@ -76,7 +77,7 @@ func (rs *SessionStore) Get(key interface{}) interface{} {
}
// Delete value in redis_sentinel 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)
@ -84,7 +85,7 @@ func (rs *SessionStore) Delete(key interface{}) error {
}
// Flush clear all values in redis_sentinel 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{})
@ -92,12 +93,12 @@ func (rs *SessionStore) Flush() error {
}
// SessionID get redis_sentinel session id
func (rs *SessionStore) SessionID() string {
func (rs *SessionStore) SessionID(context.Context) string {
return rs.sid
}
// SessionRelease save session values to redis_sentinel
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_sentinel session
// savepath like redis sentinel addr,pool size,password,dbnum,masterName
// e.g. 127.0.0.1:26379;127.0.0.2:26379,100,1qaz2wsx,0,mymaster
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 {
@ -195,7 +196,7 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
}
// SessionRead read redis_sentinel 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()
if err != nil && err != redis.Nil {
@ -214,7 +215,7 @@ func (rp *Provider) SessionRead(sid string) (session.Store, error) {
}
// SessionExist check redis_sentinel 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 {
return false, err
@ -223,7 +224,7 @@ func (rp *Provider) SessionExist(sid string) (bool, error) {
}
// SessionRegenerate generate new sid for redis_sentinel 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, err := c.Exists(oldsid).Result(); err != nil || existed == 0 {
@ -235,22 +236,22 @@ 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)
return nil
}
// 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
}

View File

@ -33,58 +33,58 @@ func TestRedisSentinel(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)
}