-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package redishelper | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
func Get[Resp any](c context.Context, cli redis.UniversalClient, key string) (*Resp, error) { | ||
cmd := cli.Get(c, key) | ||
if cmd.Err() != nil { | ||
return nil, cmd.Err() | ||
} | ||
var resp *Resp | ||
err := json.Unmarshal([]byte(cmd.String()), &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package rediscli | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/redis/go-redis/v9" | ||
"time" | ||
) | ||
|
||
type RedisClient struct { | ||
cli redis.UniversalClient | ||
conf *RedisClientConfig | ||
} | ||
|
||
func NewRedisClient(cli redis.UniversalClient, conf *RedisClientConfig) *RedisClient { | ||
if conf == nil { | ||
conf = &RedisClientConfig{} | ||
} | ||
return &RedisClient{ | ||
cli: cli, | ||
conf: conf, | ||
} | ||
} | ||
|
||
func (cli *RedisClient) Get(c context.Context, key string, resp any) error { | ||
cmd := cli.cli.Get(c, key) | ||
if cmd.Err() == nil { | ||
return cli.decode(c, key, cmd, resp) | ||
} else { | ||
return cmd.Err() | ||
} | ||
} | ||
|
||
func (cli *RedisClient) Set(c context.Context, key string, value any, expire time.Duration) error { | ||
err := cli.cli.Set(c, key, value, expire) | ||
if err.Err() != nil { | ||
return err.Err() | ||
} | ||
return nil | ||
} | ||
|
||
func (cli *RedisClient) decode(c context.Context, key string, cmd *redis.StringCmd, resp any) error { | ||
if cli.conf.Decoder != nil { | ||
return cli.conf.Decoder(c, key, cmd, resp) | ||
} | ||
buf, err := cmd.Bytes() | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(buf, resp) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type RedisClientConfig struct { | ||
Encoder func(c context.Context, key string, value any) error | ||
Decoder func(c context.Context, key string, cmd *redis.StringCmd, resp any) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package rediscli | ||
|
||
import ( | ||
"context" | ||
"github.com/redis/go-redis/v9" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func newTestClient() *RedisClient { | ||
opt := &redis.UniversalOptions{} | ||
cli := redis.NewUniversalClient(opt) | ||
return NewRedisClient(cli, nil) | ||
} | ||
|
||
func TestRedisClient_Set(t *testing.T) { | ||
cli := newTestClient() | ||
err := cli.Set(context.Background(), "a", 3, 0) | ||
require.NoError(t, err) | ||
var n int | ||
err = cli.Get(context.Background(), "a", &n) | ||
require.NoError(t, err) | ||
require.Equal(t, 3, n) | ||
} |