1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-cache\cache-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Cache\CacheBundle\DataCollector\CacheProxyInterface; |
13
|
|
|
use Cache\CacheBundle\DataCollector\TraceableAdapterEvent; |
14
|
|
|
use Psr\Cache\CacheItemInterface; |
15
|
|
|
|
16
|
|
|
class __TPL_CLASS__ extends __TPL_EXTENDS__ implements CacheProxyInterface |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
private $__name; |
19
|
|
|
private $__calls = []; |
20
|
|
|
|
21
|
|
|
public function getItem($key) |
22
|
|
|
{ |
23
|
|
|
$event = $this->start(__FUNCTION__, $key); |
24
|
|
|
|
25
|
|
|
try { |
26
|
|
|
$item = parent::getItem($key); |
27
|
|
|
} finally { |
28
|
|
|
$event->end = microtime(true); |
29
|
|
|
} |
30
|
|
|
if ($item->isHit()) { |
31
|
|
|
$event->hits++; |
32
|
|
|
} else { |
33
|
|
|
$event->misses++; |
34
|
|
|
} |
35
|
|
|
$event->result = $item->get(); |
36
|
|
|
|
37
|
|
|
return $item; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function hasItem($key) |
41
|
|
|
{ |
42
|
|
|
$event = $this->start(__FUNCTION__, $key); |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
$event->result = parent::hasItem($key); |
46
|
|
|
} finally { |
47
|
|
|
$event->end = microtime(true); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!$event->result) { |
51
|
|
|
$event->misses++; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $event->result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function deleteItem($key) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$event = $this->start(__FUNCTION__, $key); |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
return $event->result = parent::deleteItem($key); |
63
|
|
|
} finally { |
64
|
|
|
$event->end = microtime(true); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function save(CacheItemInterface $item) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$event = $this->start(__FUNCTION__, $item); |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
return $event->result = parent::save($item); |
74
|
|
|
} finally { |
75
|
|
|
$event->end = microtime(true); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function saveDeferred(CacheItemInterface $item) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$event = $this->start(__FUNCTION__, $item); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
return $event->result = parent::saveDeferred($item); |
85
|
|
|
} finally { |
86
|
|
|
$event->end = microtime(true); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getItems(array $keys = []) |
91
|
|
|
{ |
92
|
|
|
$event = $this->start(__FUNCTION__, $keys); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$result = parent::getItems($keys); |
96
|
|
|
} finally { |
97
|
|
|
$event->end = microtime(true); |
98
|
|
|
} |
99
|
|
|
$f = function () use ($result, $event) { |
100
|
|
|
$event->result = []; |
101
|
|
|
foreach ($result as $key => $item) { |
102
|
|
|
if ($item->isHit()) { |
103
|
|
|
$event->hits++; |
104
|
|
|
} else { |
105
|
|
|
$event->misses++; |
106
|
|
|
} |
107
|
|
|
$event->result[$key] = $item->get(); |
108
|
|
|
yield $key => $item; |
109
|
|
|
} |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
return $f(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function clear() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$event = $this->start(__FUNCTION__); |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
return $event->result = parent::clear(); |
121
|
|
|
} finally { |
122
|
|
|
$event->end = microtime(true); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function deleteItems(array $keys) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$event = $this->start(__FUNCTION__, $keys); |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
return $event->result = parent::deleteItems($keys); |
132
|
|
|
} finally { |
133
|
|
|
$event->end = microtime(true); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function commit() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$event = $this->start(__FUNCTION__); |
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
return $event->result = parent::commit(); |
143
|
|
|
} finally { |
144
|
|
|
$event->end = microtime(true); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function invalidateTag($tag) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$event = $this->start(__FUNCTION__, $tag); |
151
|
|
|
|
152
|
|
|
try { |
153
|
|
|
return $event->result = parent::invalidateTag($tag); |
154
|
|
|
} finally { |
155
|
|
|
$event->end = microtime(true); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function invalidateTags(array $tags) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$event = $this->start(__FUNCTION__, $tags); |
162
|
|
|
|
163
|
|
|
try { |
164
|
|
|
return $event->result = parent::invalidateTags($tags); |
165
|
|
|
} finally { |
166
|
|
|
$event->end = microtime(true); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function __getCalls() |
171
|
|
|
{ |
172
|
|
|
return $this->__calls; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function __setName($name) |
176
|
|
|
{ |
177
|
|
|
$this->__name = $name; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function start($name, $argument = null) |
181
|
|
|
{ |
182
|
|
|
$this->__calls[] = $event = new TraceableAdapterEvent(); |
183
|
|
|
$event->name = $name; |
184
|
|
|
$event->argument = $argument; |
185
|
|
|
$event->start = microtime(true); |
186
|
|
|
|
187
|
|
|
return $event; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.