Repository: zeromicro/go-zero
Pull request #4233 (2024-07-07 02:42:48)

feat/redis_hook

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/4233?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.10%. Comparing base [(`8690859`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/8690859c7d1c9461bdc422c3c65608bde30904db?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) to head [(`5b0c2e0`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/5b0c2e0bb30b24e088edfeaed18c6a6a33f68e20?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro).
> Report is 77 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/4233?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) | Coverage Δ | |
|---|---|---|
| [core/stores/redis/redis.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/4233?src=pr&el=tree&filepath=core%2Fstores%2Fredis%2Fredis.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro#diff-Y29yZS9zdG9yZXMvcmVkaXMvcmVkaXMuZ28=) | `99.82% <100.00%> (+0.04%)` | :arrow_up: |

... and [254 files with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/4233/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)

</details>
kevwan: There is a problem is that if we make this change, then go-redis is explicitly depended.

Patch:
diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go
index 76265c5a..3758877f 100644
--- a/core/stores/redis/redis.go
+++ b/core/stores/redis/redis.go
@@ -90,6 +90,18 @@ type (
 	StringCmd = red.StringCmd
 	// Script is an alias of redis.Script.
 	Script = red.Script
+
+	// Hook is an alias of redis.Hook.
+	Hook = red.Hook
+	// DialHook is an alias of redis.DialHook.
+	DialHook = red.DialHook
+	// ProcessHook is an alias of redis.ProcessHook.
+	ProcessHook = red.ProcessHook
+	// ProcessPipelineHook is an alias of redis.ProcessPipelineHook.
+	ProcessPipelineHook = red.ProcessPipelineHook
+
+	// Cmder is an alias of redis.Cmder.
+	Cmder = red.Cmder
 )
 
 // MustNewRedis returns a Redis with given options.
@@ -2363,9 +2375,9 @@ func WithTLS() Option {
 	}
 }
 
-// withHook customizes the given Redis with given durationHook, only for private use now,
+// WithHook customizes the given Redis with given durationHook, only for private use now,
 // maybe expose later.
-func withHook(hook red.Hook) Option {
+func WithHook(hook Hook) Option {
 	return func(r *Redis) {
 		r.hooks = append(r.hooks, hook)
 	}


Test patch:
diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go
index 4ddd2bfd..032ac6b8 100644
--- a/core/stores/redis/redis_test.go
+++ b/core/stores/redis/redis_test.go
@@ -160,7 +160,7 @@ func TestRedis_NonBlock(t *testing.T) {
 			Host:     s.Addr(),
 			NonBlock: true,
 			Type:     NodeType,
-		}, withHook(myHook{includePing: true}))
+		}, WithHook(myHook{includePing: true}))
 		assert.NoError(t, err)
 	})
 
@@ -170,7 +170,7 @@ func TestRedis_NonBlock(t *testing.T) {
 			Host:     s.Addr(),
 			NonBlock: false,
 			Type:     NodeType,
-		}, withHook(myHook{includePing: true}))
+		}, WithHook(myHook{includePing: true}))
 		assert.ErrorContains(t, err, "redis connect error")
 	})
 }