Browse Source

store more data in session

Paul 6 years ago
parent
commit
a679f08b62
1 changed files with 11 additions and 2 deletions
  1. 11
    2
      middleware/auth/session_redis.go

+ 11
- 2
middleware/auth/session_redis.go View File

1
 package auth
1
 package auth
2
 
2
 
3
 import (
3
 import (
4
+	"encoding/json"
4
 	"time"
5
 	"time"
5
 
6
 
6
 	"git.links123.net/links123.com/pkg/utils"
7
 	"git.links123.net/links123.com/pkg/utils"
7
 	"github.com/go-redis/redis"
8
 	"github.com/go-redis/redis"
8
 )
9
 )
9
 
10
 
10
-type RedisSessionStore struct{ client *redis.Client }
11
+type (
12
+	RedisSessionStore struct{ client *redis.Client }
13
+
14
+	item map[string]interface{}
15
+)
11
 
16
 
12
 func NewRedisSessionStore(address string, password string) *RedisSessionStore {
17
 func NewRedisSessionStore(address string, password string) *RedisSessionStore {
13
 	session := &RedisSessionStore{}
18
 	session := &RedisSessionStore{}
25
 
30
 
26
 func (rss *RedisSessionStore) StoreJwtToken(token string, uid int64, timeout int64) error {
31
 func (rss *RedisSessionStore) StoreJwtToken(token string, uid int64, timeout int64) error {
27
 	k := utils.Md5(token)
32
 	k := utils.Md5(token)
28
-	v := map[string]int64{"user_id": uid, "created": time.Now().Unix()}
33
+	v := item{"user_id": uid, "created": time.Now().Unix()}
29
 
34
 
30
 	return rss.client.Set(k, v, time.Duration(timeout)*time.Second).Err()
35
 	return rss.client.Set(k, v, time.Duration(timeout)*time.Second).Err()
31
 }
36
 }
41
 
46
 
42
 	return rss.client.Del(k).Val() == 1
47
 	return rss.client.Del(k).Val() == 1
43
 }
48
 }
49
+
50
+func (i item) MarshalBinary() (data []byte, err error) {
51
+	return json.Marshal(i)
52
+}