forked from bytewayio/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_redis.go
50 lines (40 loc) · 1.12 KB
/
session_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
49
50
package cypress
import (
"time"
"github.com/go-redis/redis"
)
type redisSessionStore struct {
redisDb *redis.Client
}
// NewRedisSessionStore creates a new redis based session store
func NewRedisSessionStore(cli *redis.Client) SessionStore {
return &redisSessionStore{cli}
}
// Close closes the store
func (store *redisSessionStore) Close() {
store.redisDb.Close()
}
// Save implements SessionStore's Save api, store the session data into redis
func (store *redisSessionStore) Save(session *Session, timeout time.Duration) error {
if !session.IsValid {
status := store.redisDb.Del(session.ID)
return status.Err()
}
data := session.Serialize()
status := store.redisDb.Set(session.ID, data, timeout)
return status.Err()
}
// Get implements SessionStore's Get api, retrieves session from redis by given id
func (store *redisSessionStore) Get(id string) (*Session, error) {
status := store.redisDb.Get(id)
if status.Err() != nil {
return nil, ErrSessionNotFound
}
data, err := status.Bytes()
if err != nil {
return nil, ErrSessionNotFound
}
session := NewSession(id)
session.Deserialize(data)
return session, nil
}