wangchuanjin 3 vuotta sitten
vanhempi
commit
5a67a44fb0

+ 2 - 0
src/github.com/go-xweb/httpsession/IsRedisStore.go

@@ -4,4 +4,6 @@ package httpsession
 var (
 	IsRedisSessionStore  = false
 	RedisSessionLockSize = 20
+	RedisNotLoginKey     = ""   //是否登录是标识
+	RedisNotLoginExpire  = 1800 //未登录用户session的失效时间为30分钟
 )

+ 21 - 5
src/github.com/go-xweb/httpsession/redissessionstore.go

@@ -43,10 +43,14 @@ func (store *redisStore) GetMultiple(id Id) map[string]interface{} {
 		lock(id).Unlock()
 		return m
 	}
-	redis.SetExpire("session", string(id), int(store.maxAge.Seconds()))
+	json.Unmarshal(*bs, &m)
+	timeout := int(store.maxAge.Seconds())
+	if RedisNotLoginKey != "" && m[RedisNotLoginKey] == nil {
+		timeout = RedisNotLoginExpire
+	}
+	redis.SetExpire("session", string(id), timeout)
 	store.last = time.Now()
 	lock(id).Unlock()
-	json.Unmarshal(*bs, &m)
 	return m
 }
 
@@ -69,8 +73,12 @@ func (store *redisStore) SetMultiple(id Id, m map[string]interface{}) {
 	for k, v := range m {
 		userdata[k] = v
 	}
+	timeout := int(store.maxAge.Seconds())
+	if RedisNotLoginKey != "" && m[RedisNotLoginKey] == nil {
+		timeout = RedisNotLoginExpire
+	}
 	putdata, _ := json.Marshal(userdata)
-	redis.PutBytes("session", string(id), &putdata, int(store.maxAge.Seconds()))
+	redis.PutBytes("session", string(id), &putdata, timeout)
 }
 
 func (store *redisStore) Add(id Id) {
@@ -87,8 +95,12 @@ func (store *redisStore) Del(id Id, key string) bool {
 	var userdata map[string]interface{}
 	json.Unmarshal(*bs, &userdata)
 	delete(userdata, key)
+	timeout := int(store.maxAge.Seconds())
+	if RedisNotLoginKey != "" && userdata[RedisNotLoginKey] == nil {
+		timeout = RedisNotLoginExpire
+	}
 	putdata, _ := json.Marshal(userdata)
-	redis.PutBytes("session", string(id), &putdata, int(store.maxAge.Seconds()))
+	redis.PutBytes("session", string(id), &putdata, timeout)
 	return true
 }
 
@@ -102,9 +114,13 @@ func (store *redisStore) UpdateByCustomField(findkey string, findvalue interface
 	}
 	var data map[string]interface{}
 	json.Unmarshal(*bs, &data)
+	timeout := int(store.maxAge.Seconds())
+	if RedisNotLoginKey != "" && data[RedisNotLoginKey] == nil {
+		timeout = RedisNotLoginExpire
+	}
 	data[setkey] = setvalue
 	putdata, _ := json.Marshal(data)
-	redis.PutBytes("session", findkey, &putdata, int(store.maxAge.Seconds()))
+	redis.PutBytes("session", findkey, &putdata, timeout)
 	return true
 }