Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
62f557af
Commit
62f557af
authored
Jul 29, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis hook
parent
46f4e6e3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
redis.go
resource/redisCli/redis.go
+2
-0
redis_hook.go
resource/redisCli/redis_hook.go
+38
-0
No files found.
resource/redisCli/redis.go
View file @
62f557af
...
...
@@ -26,6 +26,8 @@ func init() {
}
else
{
mylogrus
.
MyLog
.
Info
(
"redis db0 connection success - "
,
pong
)
}
// log hook
RedisClient
.
AddHook
(
redisHook
{})
}
func
GetRedis
()
*
redis
.
Client
{
...
...
resource/redisCli/redis_hook.go
0 → 100644
View file @
62f557af
package
redisCli
import
(
"context"
"git.hilo.cn/hilo-common/mylogrus"
"github.com/go-redis/redis/v8"
"time"
)
type
redisCost
struct
{}
var
redisCostKey
=
redisCost
{}
type
redisHook
struct
{}
func
(
redisHook
)
BeforeProcess
(
ctx
context
.
Context
,
cmd
redis
.
Cmder
)
(
context
.
Context
,
error
)
{
ctx
=
context
.
WithValue
(
ctx
,
redisCostKey
,
time
.
Now
())
return
ctx
,
nil
}
func
(
redisHook
)
AfterProcess
(
ctx
context
.
Context
,
cmd
redis
.
Cmder
)
error
{
traceId
,
userId
:=
ctx
.
Value
(
"traceId"
),
ctx
.
Value
(
"userId"
)
start
:=
ctx
.
Value
(
redisCostKey
)
var
cost
int64
if
s
,
ok
:=
start
.
(
time
.
Time
);
ok
{
cost
=
time
.
Now
()
.
Sub
(
s
)
.
Milliseconds
()
}
mylogrus
.
MyLog
.
Infof
(
"redis cmd: <%s>,err:%v traceId:%v,userId:%v,cost:%v ms"
,
cmd
.
String
(),
cmd
.
Err
(),
traceId
,
userId
,
cost
)
return
nil
}
func
(
redisHook
)
BeforeProcessPipeline
(
ctx
context
.
Context
,
cmds
[]
redis
.
Cmder
)
(
context
.
Context
,
error
)
{
return
ctx
,
nil
}
func
(
redisHook
)
AfterProcessPipeline
(
ctx
context
.
Context
,
cmds
[]
redis
.
Cmder
)
error
{
return
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment