Skip to content

Commit 4f4088a

Browse files
Added Hyperf\Redis\Event\CommandExecuted event (#7269)
Co-authored-by: 李铭昕 <[email protected]>
1 parent 973a92c commit 4f4088a

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

publish/redis.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@
4545
'options' => [
4646
'prefix' => env('REDIS_PREFIX', ''),
4747
],
48+
'event' => [
49+
'enable' => (bool) env('REDIS_EVENT_ENABLE', false),
50+
],
4851
],
4952
];

src/Event/CommandExecuted.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Redis\Event;
14+
15+
use Hyperf\Redis\RedisConnection;
16+
use Throwable;
17+
18+
class CommandExecuted
19+
{
20+
/**
21+
* Create a new event instance.
22+
* @param float $time duration in milliseconds
23+
*/
24+
public function __construct(
25+
public string $command,
26+
public array $parameters,
27+
public ?float $time,
28+
public RedisConnection $connection,
29+
public string $connectionName,
30+
public mixed $result,
31+
public ?Throwable $throwable,
32+
) {
33+
}
34+
}

src/Redis.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Hyperf\Context\Context;
1616
use Hyperf\Redis\Exception\InvalidRedisConnectionException;
1717
use Hyperf\Redis\Pool\PoolFactory;
18+
use Throwable;
1819

1920
use function Hyperf\Coroutine\defer;
2021

@@ -37,12 +38,31 @@ public function __call($name, $arguments)
3738
// Get a connection from coroutine context or connection pool.
3839
$hasContextConnection = Context::has($this->getContextKey());
3940
$connection = $this->getConnection($hasContextConnection);
41+
// Record the start time of the command.
42+
$start = (float) microtime(true);
4043

4144
try {
45+
/** @var RedisConnection $connection */
4246
$connection = $connection->getConnection();
4347
// Execute the command with the arguments.
4448
$result = $connection->{$name}(...$arguments);
49+
} catch (Throwable $exception) {
50+
throw $exception;
4551
} finally {
52+
$time = round((microtime(true) - $start) * 1000, 2);
53+
// Dispatch the command executed event.
54+
$connection->getEventDispatcher()?->dispatch(
55+
new Event\CommandExecuted(
56+
$name,
57+
$arguments,
58+
$time,
59+
$connection,
60+
$this->poolName,
61+
$result ?? null,
62+
$exception ?? null,
63+
)
64+
);
65+
4666
// Release connection.
4767
if (! $hasContextConnection) {
4868
if ($this->shouldUseSameConnection($name)) {

src/RedisConnection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Hyperf\Redis\Exception\InvalidRedisConnectionException;
2121
use Hyperf\Redis\Exception\InvalidRedisOptionException;
2222
use Psr\Container\ContainerInterface;
23+
use Psr\EventDispatcher\EventDispatcherInterface;
2324
use Psr\Log\LogLevel;
2425
use Redis;
2526
use RedisCluster;
@@ -36,6 +37,8 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
3637

3738
protected null|Redis|RedisCluster $connection = null;
3839

40+
protected ?EventDispatcherInterface $eventDispatcher = null;
41+
3942
protected array $config = [
4043
'host' => 'localhost',
4144
'port' => 6379,
@@ -62,6 +65,9 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
6265
],
6366
'options' => [],
6467
'context' => [],
68+
'event' => [
69+
'enable' => false,
70+
],
6571
];
6672

6773
/**
@@ -101,6 +107,11 @@ public function getActiveConnection()
101107
return $this;
102108
}
103109

110+
public function getEventDispatcher(): ?EventDispatcherInterface
111+
{
112+
return $this->eventDispatcher;
113+
}
114+
104115
/**
105116
* @throws RedisException
106117
* @throws ConnectionException
@@ -150,6 +161,10 @@ public function reconnect(): bool
150161
$this->connection = $redis;
151162
$this->lastUseTime = microtime(true);
152163

164+
if (($this->config['event']['enable'] ?? false) && $this->container->has(EventDispatcherInterface::class)) {
165+
$this->eventDispatcher = $this->container->get(EventDispatcherInterface::class);
166+
}
167+
153168
return true;
154169
}
155170

tests/RedisConnectionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function testRedisConnectionConfig()
8181
'context' => [
8282
'stream' => ['cafile' => 'foo-cafile', 'verify_peer' => true],
8383
],
84+
'event' => [
85+
'enable' => false,
86+
],
8487
'pool' => [
8588
'min_connections' => 1,
8689
'max_connections' => 30,

0 commit comments

Comments
 (0)