forked from itsgoingd/clockwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaravelCacheDataSource.php
155 lines (138 loc) · 4.34 KB
/
LaravelCacheDataSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php namespace Clockwork\DataSource;
use Clockwork\Helpers\Serializer;
use Clockwork\Helpers\StackTrace;
use Clockwork\Request\Request;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
/**
* Data source for Laravel cache component, provides cache queries and stats
*/
class LaravelCacheDataSource extends DataSource
{
/**
* Event dispatcher
*/
protected $eventDispatcher;
/**
* Executed cache queries
*/
protected $queries = [];
/**
* Create a new data source instance, takes an event dispatcher as argument
*/
public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* Start listening to cache events
*/
public function listenToEvents()
{
if (! class_exists(\Illuminate\Cache\Events\CacheHit::class)) {
// legacy Laravel 5.1 style events
$this->eventDispatcher->listen('cache.hit', function ($key, $value) {
$this->registerQuery([ 'type' => 'hit', 'key' => $key, 'value' => $value ]);
});
$this->eventDispatcher->listen('cache.missed', function ($key) {
$this->registerQuery([ 'type' => 'miss', 'key' => $key ]);
});
$this->eventDispatcher->listen('cache.write', function ($key, $value, $minutes) {
$this->registerQuery([
'type' => 'write', 'key' => $key, 'value' => $value, 'expiration' => $minutes * 60
]);
});
$this->eventDispatcher->listen('cache.delete', function ($key) {
$this->registerQuery([ 'type' => 'delete', 'key' => $key ]);
});
return;
}
$this->eventDispatcher->listen(\Illuminate\Cache\Events\CacheHit::class, function ($event) {
$this->registerQuery([ 'type' => 'hit', 'key' => $event->key, 'value' => $event->value ]);
});
$this->eventDispatcher->listen(\Illuminate\Cache\Events\CacheMissed::class, function ($event) {
$this->registerQuery([ 'type' => 'miss', 'key' => $event->key ]);
});
$this->eventDispatcher->listen(\Illuminate\Cache\Events\KeyWritten::class, function ($event) {
$this->registerQuery([
'type' => 'write', 'key' => $event->key, 'value' => $event->value, 'expiration' => $event->minutes * 60
]);
});
$this->eventDispatcher->listen(\Illuminate\Cache\Events\KeyForgotten::class, function ($event) {
$this->registerQuery([ 'type' => 'delete', 'key' => $event->key ]);
});
}
/**
* Adds cache queries and stats to the request
*/
public function resolve(Request $request)
{
$request->cacheQueries = array_merge($request->cacheQueries, $this->getQueries());
$request->cacheReads = $request->cacheReads + $this->getCacheReads();
$request->cacheHits = $request->cacheHits + $this->getCacheHits();
$request->cacheWrites = $request->cacheWrites + $this->getCacheWrites();
$request->cacheDeletes = $request->cacheDeletes + $this->getCacheDeletes();
return $request;
}
/**
* Registers a new query, resolves caller file and line no
*/
public function registerQuery(array $query)
{
$trace = StackTrace::get();
$caller = $trace->firstNonVendor([ 'itsgoingd', 'laravel', 'illuminate' ]);
$this->queries[] = array_merge($query, [
'file' => $caller->shortPath,
'line' => $caller->line,
'trace' => $this->collectStackTraces ? Serializer::trace($trace->framesBefore($caller)) : null
]);
}
/**
* Returns an array of cache queries in Clockwork metadata format
*/
protected function getQueries()
{
return array_map(function ($query) {
return array_merge($query, [
'connection' => null,
'time' => null,
'value' => isset($query['value']) ? Serializer::simplify($query['value']) : null
]);
}, $this->queries);
}
/**
* Returns a number of cache reads (hits + misses)
*/
protected function getCacheReads()
{
return count(array_filter($this->queries, function ($query) {
return $query['type'] == 'hit' || $query['type'] == 'miss';
}));
}
/**
* Returns a number of cache hits
*/
protected function getCacheHits()
{
return count(array_filter($this->queries, function ($query) {
return $query['type'] == 'hit';
}));
}
/**
* Returns a number of cache writes
*/
protected function getCacheWrites()
{
return count(array_filter($this->queries, function ($query) {
return $query['type'] == 'write';
}));
}
/**
* Returns a number of cache deletes
*/
protected function getCacheDeletes()
{
return count(array_filter($this->queries, function ($query) {
return $query['type'] == 'delete';
}));
}
}