-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileCachePool.php
More file actions
215 lines (187 loc) · 5.3 KB
/
ProfileCachePool.php
File metadata and controls
215 lines (187 loc) · 5.3 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* PHP Cache Profiler.
*
* This is a wrapper class for cache pools that measures cache hits, misses, etc.
*
* Developed by Corollarium Technologies
* https://corollarium.com
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Corollarium\Cache;
class ProfileCachePool implements \Psr\Cache\CacheItemPoolInterface
{
const ACCESSED = 'accessed';
const MISSED = 'missed';
const DELETED = 'deleted';
const CLEANED = 'cleaned';
const SAVED = 'saved';
/**
* Array for basic cache profiling.
* Keys are Consts of this class, values are counters.
*
* @var array
*/
protected $profile = [];
/**
* Returns basic cache statistics.
* See $summary.
*
* @return array()
*/
public function getProfileSummary()
{
return $this->profile;
}
/**
*
* @return number[]
*/
private static function zeroStructure() {
return array(
static::ACCESSED => 0,
static::MISSED => 0,
static::DELETED => 0,
static::CLEANED => 0,
static::SAVED => 0
);
}
public function resetProfileSummary()
{
$this->profile = static::zeroStructure();
}
/**
*
* @param ProfileCachePool[] $poolInterfaces
*/
public static function mergeProfileSummaries(array $profileInterfaces)
{
$total = static::zeroStructure();
foreach ($profileInterfaces as $c) {
if ($c instanceof ProfileCachePool) {
$s = $c->getProfileSummary();
foreach ($s as $k => $v) {
$total[$k] += $v;
}
}
}
return $total;
}
/**
* Generates a simple report in HTML. Run this in the footer of your page.
*/
public function reportHTML()
{
echo '<style>
.cache-success { background-color: #468847; border-radius: 3px; color: #FFF; padding: 2px 4px; }
.cache-miss { background-color: #B94A48; border-radius: 3px; color: #FFF; padding: 2px 4px; }
.cache-save { background-color: #0694F8; border-radius: 3px; color: #FFF; padding: 2px 4px; }
.cache-deleted { background-color: #F89406; border-radius: 3px; color: #FFF; padding: 2px 4px; }
.cache-cleaned { background-color: #F894F8; border-radius: 3px; color: #FFF; padding: 2px 4px; }
</style>';
$name = get_class($this->pool);
echo '<div class="cache-summary"><h2>Cache ' . $name . ' system</h2>';
$stats = static::zeroStructure();
echo '<ul>';
foreach ($stats as $key => $val) {
echo '<li>' . $key . '=' . $val . '</li>';
}
echo '</ul>';
echo '</div>';
}
/**
* Prints a summary
*
* @param array $summary An array like the one returned by getProfileSummary()
*/
public static function summaryHTML($summary)
{
echo '<div id="cache-summary">Cache Profile Summary: ';
foreach ($summary as $key => $val) {
echo $key . '=>' . $val . ' / ';
}
echo '</div>';
}
/**
*
* @var \Psr\Cache\CacheItemPoolInterface
*/
protected $pool;
public function __construct(\Psr\Cache\CacheItemPoolInterface $pool)
{
$this->profile = static::zeroStructure();
$this->pool = $pool;
}
public function getItem($key)
{
$item = $this->pool->getItem($key);
$this->profile[static::ACCESSED] ++;
if (! $item->isHit()) {
$this->profile[static::MISSED] ++;
}
return $item;
}
public function getItems(array $keys = array())
{
$items = $this->pool->getItems($keys);
foreach ($items as $key => $item) {
$this->profile[static::ACCESSED] ++;
if (! $item->isHit()) {
$this->profile[static::MISSED] ++;
}
}
return $items;
}
public function hasItem($key)
{
return $this->pool->hasItem($key);
}
public function clear()
{
$retval = $this->pool->clear();
if ($retval) {
$this->profile[static::CLEANED] ++;
}
return $this->pool->clear();
}
public function deleteItem($key)
{
$retval = $this->pool->deleteItem($key);
if ($retval) {
$this->profile[static::DELETED] ++;
}
return $retval;
}
public function deleteItems(array $keys)
{
$retval = $this->pool->deleteItems($keys);
foreach ($retval as $r) {
if ($r) {
$this->profile[static::DELETED] ++;
}
}
return $retval;
}
public function save(\Psr\Cache\CacheItemInterface $item)
{
$retval = $this->pool->save($item);
if ($retval) {
$this->profile[static::SAVED] ++;
}
return $retval;
}
public function saveDeferred(\Psr\Cache\CacheItemInterface $item)
{
$retval = $this->pool->saveDeferred($item);
if ($retval) {
// we're not sure it's going to be saved, but let's count it anyway.
$this->profile[static::SAVED] ++;
}
return $retval;
}
public function commit()
{
return $this->pool->commit();
}
}