Skip to content

Commit 1baee85

Browse files
authored
redis: support setting key prefix (#94)
1 parent 51bcaa5 commit 1baee85

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

redis/redis.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,31 @@ var _ session.Store = (*redisStore)(nil)
1919

2020
// redisStore is a Redis implementation of the session store.
2121
type redisStore struct {
22-
client *redis.Client // The client connection
23-
lifetime time.Duration // The duration to have access to a session before being recycled
24-
encoder session.Encoder // The encoder to encode the session data before saving
25-
decoder session.Decoder // The decoder to decode binary to session data after reading
22+
client *redis.Client // The client connection
23+
keyPrefix string // The prefix to use for keys
24+
lifetime time.Duration // The duration to have access to a session before being recycled
25+
encoder session.Encoder // The encoder to encode the session data before saving
26+
decoder session.Decoder // The decoder to decode binary to session data after reading
2627
}
2728

2829
// newRedisStore returns a new Redis session store based on given configuration.
2930
func newRedisStore(cfg Config) *redisStore {
3031
return &redisStore{
31-
client: cfg.client,
32-
lifetime: cfg.Lifetime,
33-
encoder: cfg.Encoder,
34-
decoder: cfg.Decoder,
32+
client: cfg.client,
33+
keyPrefix: cfg.KeyPrefix,
34+
lifetime: cfg.Lifetime,
35+
encoder: cfg.Encoder,
36+
decoder: cfg.Decoder,
3537
}
3638
}
3739

3840
func (s *redisStore) Exist(ctx context.Context, sid string) bool {
39-
result, err := s.client.Exists(ctx, sid).Result()
41+
result, err := s.client.Exists(ctx, s.keyPrefix+sid).Result()
4042
return err == nil && result == 1
4143
}
4244

4345
func (s *redisStore) Read(ctx context.Context, sid string) (session.Session, error) {
44-
binary, err := s.client.Get(ctx, sid).Result()
46+
binary, err := s.client.Get(ctx, s.keyPrefix+sid).Result()
4547
if err != nil {
4648
if err == redis.Nil {
4749
return session.NewBaseSession(sid, s.encoder), nil
@@ -57,11 +59,11 @@ func (s *redisStore) Read(ctx context.Context, sid string) (session.Session, err
5759
}
5860

5961
func (s *redisStore) Destroy(ctx context.Context, sid string) error {
60-
return s.client.Del(ctx, sid).Err()
62+
return s.client.Del(ctx, s.keyPrefix+sid).Err()
6163
}
6264

6365
func (s *redisStore) Touch(ctx context.Context, sid string) error {
64-
err := s.client.Expire(ctx, sid, s.lifetime).Err()
66+
err := s.client.Expire(ctx, s.keyPrefix+sid, s.lifetime).Err()
6567
if err != nil {
6668
return errors.Wrap(err, "expire")
6769
}
@@ -74,7 +76,7 @@ func (s *redisStore) Save(ctx context.Context, sess session.Session) error {
7476
return errors.Wrap(err, "encode")
7577
}
7678

77-
err = s.client.SetEX(ctx, sess.ID(), binary, s.lifetime).Err()
79+
err = s.client.SetEX(ctx, s.keyPrefix+sess.ID(), binary, s.lifetime).Err()
7880
if err != nil {
7981
return errors.Wrap(err, "set")
8082
}
@@ -95,6 +97,8 @@ type Config struct {
9597

9698
// Options is the settings to set up Redis client connection.
9799
Options *Options
100+
// KeyPrefix is the prefix to use for keys in Redis. Default is "session:".
101+
KeyPrefix string
98102
// Lifetime is the duration to have no access to a session before being
99103
// recycled. Default is 3600 seconds.
100104
Lifetime time.Duration
@@ -125,6 +129,9 @@ func Initer() session.Initer {
125129
cfg.client = redis.NewClient(cfg.Options)
126130
}
127131

132+
if cfg.KeyPrefix == "" {
133+
cfg.KeyPrefix = "session:"
134+
}
128135
if cfg.Lifetime.Seconds() < 1 {
129136
cfg.Lifetime = 3600 * time.Second
130137
}

0 commit comments

Comments
 (0)