Skip to content

Commit

Permalink
feat: allow sharing the underlying Client of a rueidisaside.CacheAsid…
Browse files Browse the repository at this point in the history
…eClient
  • Loading branch information
rueian committed Nov 24, 2023
1 parent f81f163 commit cfe1cc0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions rueidisaside/aside.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ClientOption struct {
type CacheAsideClient interface {
Get(ctx context.Context, ttl time.Duration, key string, fn func(ctx context.Context, key string) (val string, err error)) (val string, err error)
Del(ctx context.Context, key string) error
Client() rueidis.Client
Close()
}

Expand Down Expand Up @@ -177,6 +178,11 @@ func (c *Client) Del(ctx context.Context, key string) error {
return c.client.Do(ctx, c.client.B().Del().Key(key).Build()).Error()
}

// Client exports the underlying rueidis.Client
func (c *Client) Client() rueidis.Client {
return c.client
}

func (c *Client) Close() {
c.cancel()
c.mu.Lock()
Expand Down
13 changes: 9 additions & 4 deletions rueidisaside/aside_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,21 @@ func TestClientErr(t *testing.T) {
}

func TestWithClientBuilder(t *testing.T) {
client, err := NewClient(ClientOption{
var client rueidis.Client
c, err := NewClient(ClientOption{
ClientOption: rueidis.ClientOption{InitAddress: addr},
ClientBuilder: func(option rueidis.ClientOption) (rueidis.Client, error) {
return rueidis.NewClient(option)
ClientBuilder: func(option rueidis.ClientOption) (_ rueidis.Client, err error) {
client, err = rueidis.NewClient(option)
return client, err
},
})
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer c.Close()
if c.Client() != client {
t.Fatal("client mismatched")
}
}

func TestCacheFilled(t *testing.T) {
Expand Down

0 comments on commit cfe1cc0

Please sign in to comment.