...
1 package redis
2
3 import (
4 "errors"
5
6 "github.com/boj/redistore"
7 "github.com/gin-contrib/sessions"
8 "github.com/gomodule/redigo/redis"
9 )
10
11 type Store interface {
12 sessions.Store
13 }
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 func NewStore(size int, network, address, password string, keyPairs ...[]byte) (Store, error) {
29 s, err := redistore.NewRediStore(size, network, address, password, keyPairs...)
30 if err != nil {
31 return nil, err
32 }
33 return &store{s}, nil
34 }
35
36
37
38
39
40 func NewStoreWithDB(size int, network, address, password, DB string, keyPairs ...[]byte) (Store, error) {
41 s, err := redistore.NewRediStoreWithDB(size, network, address, password, DB, keyPairs...)
42 if err != nil {
43 return nil, err
44 }
45 return &store{s}, nil
46 }
47
48
49
50
51 func NewStoreWithPool(pool *redis.Pool, keyPairs ...[]byte) (Store, error) {
52 s, err := redistore.NewRediStoreWithPool(pool, keyPairs...)
53 if err != nil {
54 return nil, err
55 }
56 return &store{s}, nil
57 }
58
59 type store struct {
60 *redistore.RediStore
61 }
62
63
64
65 func GetRedisStore(s Store) (err error, rediStore *redistore.RediStore) {
66 realStore, ok := s.(*store)
67 if !ok {
68 err = errors.New("unable to get the redis store: Store isn't *store")
69 return
70 }
71
72 rediStore = realStore.RediStore
73 return
74 }
75
76
77 func SetKeyPrefix(s Store, prefix string) error {
78 err, rediStore := GetRedisStore(s)
79 if err != nil {
80 return err
81 }
82
83 rediStore.SetKeyPrefix(prefix)
84 return nil
85 }
86
87 func (c *store) Options(options sessions.Options) {
88 c.RediStore.Options = options.ToGorillaOptions()
89 }
90
View as plain text