112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// Client is wrapper interface for redis.Client and redis.ClusterClient.
|
|
type Client interface {
|
|
Get(ctx context.Context, key string) ([]byte, error)
|
|
Lock(key string) sessions.Lock
|
|
Expire(ctx context.Context, key string, expiration time.Duration) error
|
|
RPush(ctx context.Context, key string, value string) error
|
|
LRange(ctx context.Context, key string) ([]string, error)
|
|
Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
|
|
Del(ctx context.Context, key string) error
|
|
Ping(ctx context.Context) error
|
|
}
|
|
|
|
var _ Client = (*client)(nil)
|
|
|
|
type client struct {
|
|
*redis.Client
|
|
}
|
|
|
|
func newClient(c *redis.Client) Client {
|
|
return &client{
|
|
Client: c,
|
|
}
|
|
}
|
|
|
|
func (c *client) Expire(ctx context.Context, key string, expiration time.Duration) error {
|
|
return c.Client.Expire(ctx, key, expiration).Err()
|
|
}
|
|
|
|
func (c *client) LRange(ctx context.Context, key string) ([]string, error) {
|
|
return c.Client.LRange(ctx, key, 0, -1).Result()
|
|
}
|
|
|
|
func (c *client) RPush(ctx context.Context, key string, value string) error {
|
|
return c.Client.RPush(ctx, key, value).Err()
|
|
}
|
|
|
|
func (c *client) Get(ctx context.Context, key string) ([]byte, error) {
|
|
return c.Client.Get(ctx, key).Bytes()
|
|
}
|
|
|
|
func (c *client) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
|
|
return c.Client.Set(ctx, key, value, expiration).Err()
|
|
}
|
|
|
|
func (c *client) Del(ctx context.Context, key string) error {
|
|
return c.Client.Del(ctx, key).Err()
|
|
}
|
|
|
|
func (c *client) Lock(key string) sessions.Lock {
|
|
return NewLock(c.Client, key)
|
|
}
|
|
|
|
func (c *client) Ping(ctx context.Context) error {
|
|
return c.Client.Ping(ctx).Err()
|
|
}
|
|
|
|
var _ Client = (*clusterClient)(nil)
|
|
|
|
type clusterClient struct {
|
|
*redis.ClusterClient
|
|
}
|
|
|
|
func newClusterClient(c *redis.ClusterClient) Client {
|
|
return &clusterClient{
|
|
ClusterClient: c,
|
|
}
|
|
}
|
|
|
|
// Expire implements Client.
|
|
// Subtle: this method shadows the method (*ClusterClient).Expire of clusterClient.ClusterClient.
|
|
func (c *clusterClient) Expire(ctx context.Context, key string, expiration time.Duration) error {
|
|
return c.ClusterClient.Expire(ctx, key, expiration).Err()
|
|
}
|
|
|
|
func (c *clusterClient) Get(ctx context.Context, key string) ([]byte, error) {
|
|
return c.ClusterClient.Get(ctx, key).Bytes()
|
|
}
|
|
|
|
func (c *clusterClient) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
|
|
return c.ClusterClient.Set(ctx, key, value, expiration).Err()
|
|
}
|
|
|
|
func (c *clusterClient) RPush(ctx context.Context, key string, value string) error {
|
|
return c.ClusterClient.RPush(ctx, key, value).Err()
|
|
}
|
|
|
|
func (c *clusterClient) LRange(ctx context.Context, key string) ([]string, error) {
|
|
return c.ClusterClient.LRange(ctx, key, 0, -1).Result()
|
|
}
|
|
|
|
func (c *clusterClient) Del(ctx context.Context, key string) error {
|
|
return c.ClusterClient.Del(ctx, key).Err()
|
|
}
|
|
|
|
func (c *clusterClient) Lock(key string) sessions.Lock {
|
|
return NewLock(c.ClusterClient, key)
|
|
}
|
|
|
|
func (c *clusterClient) Ping(ctx context.Context) error {
|
|
return c.ClusterClient.Ping(ctx).Err()
|
|
}
|