-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
48 lines (43 loc) · 1.06 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package stores
import (
"fmt"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/logs"
"time"
)
type Redis struct {
lifetime time.Duration
redis contracts.RedisConnection
key string
}
func RedisStore(key string, lifetime time.Duration, redis contracts.RedisConnection) contracts.SessionStore {
return &Redis{
lifetime: lifetime,
key: key,
redis: redis,
}
}
func (this *Redis) LoadSession(id string) map[string]string {
sessions, err := this.redis.HGetAll(fmt.Sprintf(this.key, id))
if err != nil {
logs.WithError(err).
WithField("key", fmt.Sprintf(this.key, id)).
Warn("LoadSession err")
}
if sessions == nil {
return make(map[string]string)
}
return sessions
}
func (this *Redis) Save(id string, sessions map[string]string) {
values := make([]interface{}, 0)
for key, value := range sessions {
values = append(values, key, value)
}
_, err := this.redis.HMSet(fmt.Sprintf(this.key, id), values...)
if err != nil {
logs.WithError(err).
WithField("key", fmt.Sprintf(this.key, id)).
Warn("session save err")
}
}