Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: #3191 allow custom and overwrite the Do method implements #3194

Merged
merged 7 commits into from
Dec 28, 2023

Conversation

hailaz
Copy link
Member

@hailaz hailaz commented Dec 8, 2023

#3191

package main

import (
	"context"
	"fmt"

	"github.com/gogf/gf/contrib/nosql/redis/v2"

	"github.com/gogf/gf/v2/container/gvar"
	"github.com/gogf/gf/v2/database/gredis"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)

var (
	ctx    = gctx.New()
	group  = "cache"
	config = gredis.Config{
		Address: "127.0.0.1:6379",
		Db:      1,
	}
)

// MyRedis description
type MyRedis struct {
	*redis.Redis
}

// Do implements and overwrites the underlying function Do from Adapter.
func (r *MyRedis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
	fmt.Println("MyRedis Do:", command, args)
	return r.Redis.Do(ctx, command, args...)
}

func main() {
	gredis.RegisterAdapterFunc(func(config *gredis.Config) gredis.Adapter {
		r := &MyRedis{redis.New(config)}
		r.AdapterOperation = r // This is necessary.
		return r
	})
	gredis.SetConfig(&config, group)

	_, err := g.Redis(group).Set(ctx, "key", "value")
	if err != nil {
		g.Log().Fatal(ctx, err)
	}
	value, err := g.Redis(group).Get(ctx, "key")
	if err != nil {
		g.Log().Fatal(ctx, err)
	}
	fmt.Println(value.String())
}

@gqcn gqcn changed the title fix: redis实现的全部分组不公开成员则无法覆盖内部的Do fix #3191 Dec 18, 2023
@gqcn gqcn added the wip label Dec 20, 2023
@gqcn gqcn force-pushed the fix/redis-adapter branch from c2f50f8 to 4e0836b Compare December 20, 2023 12:59
@gqcn gqcn removed the wip label Dec 20, 2023
@hailaz hailaz requested a review from gqcn December 21, 2023 09:16
@hailaz hailaz added the done This issue is done, which may be release in next version. label Dec 21, 2023
@gqcn gqcn added ready to merge Used in PR, which means this PR is reviewed. and removed done This issue is done, which may be release in next version. labels Dec 21, 2023
@gqcn gqcn merged commit 63bdf41 into master Dec 28, 2023
42 checks passed
@gqcn gqcn deleted the fix/redis-adapter branch December 28, 2023 12:18
@gqcn gqcn changed the title fix #3191 fix: #3191 allow custom and overwrite the Do method Apr 7, 2024
@gqcn gqcn changed the title fix: #3191 allow custom and overwrite the Do method fix: #3191 allow custom and overwrite the Do method implements Apr 7, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
ready to merge Used in PR, which means this PR is reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants