Răsfoiți Sursa

修改session

wangchuanjin 2 ani în urmă
părinte
comite
ca44b91a70

+ 6 - 4
go-xweb/httpsession/memorystore.go

@@ -53,9 +53,11 @@ func (node *sessionNode) SetMultiple(m map[string]interface{}) {
 	node.lock.Unlock()
 }
 
-func (node *sessionNode) Del(key string) {
+func (node *sessionNode) Del(keys ...string) {
 	node.lock.Lock()
-	delete(node.kvs, key)
+	for _, v := range keys {
+		delete(node.kvs, v)
+	}
 	node.last = time.Now()
 	node.lock.Unlock()
 }
@@ -191,12 +193,12 @@ func (store *MemoryStore) Add(id Id) {
 	store.lock.Unlock()
 }
 
-func (store *MemoryStore) Del(id Id, key string) bool {
+func (store *MemoryStore) Del(id Id, keys ...string) bool {
 	store.lock.RLock()
 	node, ok := store.nodes[id]
 	store.lock.RUnlock()
 	if ok {
-		node.Del(key)
+		node.Del(keys...)
 	}
 	return true
 }

+ 4 - 2
go-xweb/httpsession/redissessionstore.go

@@ -86,7 +86,7 @@ func (store *redisStore) Add(id Id) {
 
 }
 
-func (store *redisStore) Del(id Id, key string) bool {
+func (store *redisStore) Del(id Id, keys ...string) bool {
 	lock(id).Lock()
 	defer lock(id).Unlock()
 	bs, err := redis.GetBytes("session", string(id))
@@ -95,7 +95,9 @@ func (store *redisStore) Del(id Id, key string) bool {
 	}
 	var userdata map[string]interface{}
 	json.Unmarshal(*bs, &userdata)
-	delete(userdata, key)
+	for _, key := range keys {
+		delete(userdata, key)
+	}
 	timeout := int(store.maxAge.Seconds())
 	if RedisNotLoginKey != "" && userdata[RedisNotLoginKey] == nil {
 		timeout = RedisNotLoginExpire

+ 2 - 2
go-xweb/httpsession/session.go

@@ -35,8 +35,8 @@ func (session *Session) SetMultiple(m map[string]interface{}) {
 	session.manager.store.SetMultiple(session.id, m)
 }
 
-func (session *Session) Del(key string) bool {
-	return session.manager.store.Del(session.id, key)
+func (session *Session) Del(keys ...string) bool {
+	return session.manager.store.Del(session.id, keys...)
 }
 
 func (session *Session) Invalidate(rw http.ResponseWriter) {

+ 1 - 1
go-xweb/httpsession/store.go

@@ -9,7 +9,7 @@ type Store interface {
 	GetMultiple(id Id) map[string]interface{}
 	Set(id Id, key string, value interface{})
 	SetMultiple(id Id, m map[string]interface{})
-	Del(id Id, key string) bool
+	Del(id Id, keys ...string) bool
 	Clear(id Id) bool
 	Add(id Id)
 	Exist(id Id) bool