| 199 |
lars |
1 |
<?php namespace Clockwork\DataSource;
|
|
|
2 |
|
|
|
3 |
use Clockwork\Helpers\Serializer;
|
|
|
4 |
use Clockwork\Helpers\StackTrace;
|
|
|
5 |
use Clockwork\Request\Request;
|
|
|
6 |
|
|
|
7 |
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
|
|
|
8 |
|
|
|
9 |
// Data source for Laravel redis component, provides redis commands
|
|
|
10 |
class LaravelRedisDataSource extends DataSource
|
|
|
11 |
{
|
|
|
12 |
// Event dispatcher instance
|
|
|
13 |
protected $eventDispatcher;
|
|
|
14 |
|
|
|
15 |
// Executed redis commands
|
|
|
16 |
protected $commands = [];
|
|
|
17 |
|
|
|
18 |
// Whether to skip Redis commands originating from Laravel cache Redis store
|
|
|
19 |
protected $skipCacheCommands = true;
|
|
|
20 |
|
|
|
21 |
// Create a new data source instance, takes an event dispatcher and additional options as arguments
|
|
|
22 |
public function __construct(EventDispatcher $eventDispatcher, $skipCacheCommands = true)
|
|
|
23 |
{
|
|
|
24 |
$this->eventDispatcher = $eventDispatcher;
|
|
|
25 |
|
|
|
26 |
$this->skipCacheCommands = $skipCacheCommands;
|
|
|
27 |
|
|
|
28 |
if ($this->skipCacheCommands) {
|
|
|
29 |
$this->addFilter(function ($command, $trace) {
|
|
|
30 |
return ! $trace->first(function ($frame) { return $frame->class == 'Illuminate\Cache\RedisStore'; });
|
|
|
31 |
});
|
|
|
32 |
}
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
// Adds redis commands to the request
|
|
|
36 |
public function resolve(Request $request)
|
|
|
37 |
{
|
|
|
38 |
$request->redisCommands = array_merge($request->redisCommands, $this->getCommands());
|
|
|
39 |
|
|
|
40 |
return $request;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
// Reset the data source to an empty state, clearing any collected data
|
|
|
44 |
public function reset()
|
|
|
45 |
{
|
|
|
46 |
$this->commands = [];
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// Listen to the cache events
|
|
|
50 |
public function listenToEvents()
|
|
|
51 |
{
|
|
|
52 |
$this->eventDispatcher->listen(\Illuminate\Redis\Events\CommandExecuted::class, function ($event) {
|
|
|
53 |
$this->registerCommand([
|
|
|
54 |
'command' => $event->command,
|
|
|
55 |
'parameters' => $event->parameters,
|
|
|
56 |
'duration' => $event->time,
|
|
|
57 |
'connection' => $event->connectionName,
|
|
|
58 |
'time' => microtime(true) - $event->time / 1000
|
|
|
59 |
]);
|
|
|
60 |
});
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// Collect an executed command
|
|
|
64 |
protected function registerCommand(array $command)
|
|
|
65 |
{
|
|
|
66 |
$trace = StackTrace::get()->resolveViewName();
|
|
|
67 |
|
|
|
68 |
$command = array_merge($command, [
|
|
|
69 |
'trace' => (new Serializer)->trace($trace)
|
|
|
70 |
]);
|
|
|
71 |
|
|
|
72 |
if ($this->passesFilters([ $command, $trace ])) {
|
|
|
73 |
$this->commands[] = $command;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// Get an array of executed redis commands
|
|
|
78 |
protected function getCommands()
|
|
|
79 |
{
|
|
|
80 |
return array_map(function ($query) {
|
|
|
81 |
return array_merge($query, [
|
|
|
82 |
'parameters' => isset($query['parameters']) ? (new Serializer)->normalize($query['parameters']) : null
|
|
|
83 |
]);
|
|
|
84 |
}, $this->commands);
|
|
|
85 |
}
|
|
|
86 |
}
|