Files
gf/database/gredis/gredis_adapter.go
云知易客 41a5484620 fix(database/gredis): gredis support get raw client (#4306)
Fixes https://github.com/gogf/gf/issues/4298
Fixes https://github.com/gogf/gf/issues/2196
Fixes https://github.com/gogf/gf/issues/2135
```go
import goredis "github.com/redis/go-redis/v9"

func ExampleUsage(ctx context.Context, redis *Redis) error {
	client := redis.Client()
	universalClient, ok := client.(goredis.UniversalClient)
	if !ok {
		return errors.New("failed to assert to UniversalClient")
	}

	// Use universalClient for advanced operations like Pipeline
	pipe := universalClient.Pipeline()
	pipe.Set(ctx, "key1", "value1", 0)
	pipe.Set(ctx, "key2", "value2", 0)
	results, err := pipe.Exec(ctx)
	if err != nil {
		return err
	}
	// ... handle results
	return nil
}
```

---------

Co-authored-by: hailaz <739476267@qq.com>
2025-09-03 16:09:43 +08:00

93 lines
3.4 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gredis
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
)
// Adapter is an interface for universal redis operations.
type Adapter interface {
AdapterGroup
AdapterOperation
}
// AdapterGroup is an interface managing group operations for redis.
type AdapterGroup interface {
GroupGeneric() IGroupGeneric
GroupHash() IGroupHash
GroupList() IGroupList
GroupPubSub() IGroupPubSub
GroupScript() IGroupScript
GroupSet() IGroupSet
GroupSortedSet() IGroupSortedSet
GroupString() IGroupString
}
// RedisRawClient is a type alias for any, representing the raw underlying redis client.
// Implementations should return their concrete client type as this interface.
type RedisRawClient any
// AdapterOperation is the core operation functions for redis.
// These functions can be easily overwritten by custom implements.
type AdapterOperation interface {
// Do send a command to the server and returns the received reply.
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
Do(ctx context.Context, command string, args ...any) (*gvar.Var, error)
// Conn retrieves and returns a connection object for continuous operations.
// Note that you should call Close function manually if you do not use this connection any further.
Conn(ctx context.Context) (conn Conn, err error)
// Close closes current redis client, closes its connection pool and releases all its related resources.
Close(ctx context.Context) (err error)
// Client returns the underlying redis client instance.
// This method provides access to the raw redis client for advanced operations
// that are not covered by the standard redis adapter interface.
Client() RedisRawClient
}
// Conn is an interface of a connection from universal redis client.
type Conn interface {
ConnCommand
// Do send a command to the server and returns the received reply.
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
Do(ctx context.Context, command string, args ...any) (result *gvar.Var, err error)
// Close puts the connection back to connection pool.
Close(ctx context.Context) (err error)
}
// ConnCommand is an interface managing some operations bound to certain connection.
type ConnCommand interface {
// Subscribe subscribes the client to the specified channels.
// https://redis.io/commands/subscribe/
Subscribe(ctx context.Context, channel string, channels ...string) ([]*Subscription, error)
// PSubscribe subscribes the client to the given patterns.
//
// Supported glob-style patterns:
// - h?llo subscribes to hello, hallo and hxllo
// - h*llo subscribes to hllo and heeeello
// - h[ae]llo subscribes to hello and hallo, but not hillo
//
// Use \ to escape special characters if you want to match them verbatim.
//
// https://redis.io/commands/psubscribe/
PSubscribe(ctx context.Context, pattern string, patterns ...string) ([]*Subscription, error)
// ReceiveMessage receives a single message of subscription from the Redis server.
ReceiveMessage(ctx context.Context) (*Message, error)
// Receive receives a single reply as gvar.Var from the Redis server.
Receive(ctx context.Context) (result *gvar.Var, err error)
}