Skip to content

Commit 5d751e6

Browse files
committed
Updated statistics and make sure LoggingCachePool is Taggable
1 parent 38a5e25 commit 5d751e6

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

src/Cache/LoggingCachePool.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
namespace Cache\CacheBundle\Cache;
1313

14+
use Cache\Taggable\TaggablePoolInterface;
1415
use Psr\Cache\CacheItemInterface;
1516
use Psr\Cache\CacheItemPoolInterface;
1617

1718
/**
1819
* @author Aaron Scherer <[email protected]>
1920
*/
20-
class LoggingCachePool implements CacheItemPoolInterface
21+
class LoggingCachePool implements CacheItemPoolInterface, TaggablePoolInterface
2122
{
2223
/**
2324
* @type array
@@ -39,28 +40,50 @@ public function __construct(CacheItemPoolInterface $cachePool)
3940
$this->cachePool = $cachePool;
4041
}
4142

42-
public function getItem($key)
43+
/**
44+
* @param string $name
45+
* @param array $arguments
46+
*
47+
* @return object
48+
*/
49+
private function timeCall($name, array $arguments = null)
4350
{
44-
$call = $this->timeCall(__FUNCTION__, [$key]);
51+
$start = microtime(true);
52+
$result = call_user_func_array([$this->cachePool, $name], $arguments);
53+
$time = microtime(true) - $start;
54+
55+
$object = (object) compact('name', 'arguments', 'start', 'time', 'result');
56+
57+
return $object;
58+
}
59+
60+
public function getItem($key, array $tags = [])
61+
{
62+
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
4563
$result = $call->result;
46-
$call->result = sprintf('<DATA:%s>', gettype($result));
64+
65+
if ($result->isHit()) {
66+
$call->result = sprintf('<DATA:%s>', gettype($result->get()));
67+
} else {
68+
$call->result = false;
69+
}
4770

4871
$this->calls[] = $call;
4972

5073
return $result;
5174
}
5275

53-
public function hasItem($key)
76+
public function hasItem($key, array $tags = [])
5477
{
55-
$call = $this->timeCall(__FUNCTION__, [$key]);
78+
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
5679
$this->calls[] = $call;
5780

5881
return $call->result;
5982
}
6083

61-
public function deleteItem($key)
84+
public function deleteItem($key, array $tags = [])
6285
{
63-
$call = $this->timeCall(__FUNCTION__, [$key]);
86+
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
6487
$this->calls[] = $call;
6588

6689
return $call->result;
@@ -78,26 +101,9 @@ public function save(CacheItemInterface $item)
78101
return $call->result;
79102
}
80103

81-
/**
82-
* @param string $name
83-
* @param array $arguments
84-
*
85-
* @return object
86-
*/
87-
private function timeCall($name, array $arguments = null)
88-
{
89-
$start = microtime(true);
90-
$result = call_user_func_array([$this->cachePool, $name], $arguments);
91-
$time = microtime(true) - $start;
92-
93-
$object = (object) compact('name', 'arguments', 'start', 'time', 'result');
94-
95-
return $object;
96-
}
97-
98-
public function getItems(array $keys = [])
104+
public function getItems(array $keys = [], array $tags = [])
99105
{
100-
$call = $this->timeCall(__FUNCTION__, [$keys]);
106+
$call = $this->timeCall(__FUNCTION__, [$keys, $tags]);
101107
$result = $call->result;
102108
$call->result = sprintf('<DATA:%s>', gettype($result));
103109

@@ -106,17 +112,17 @@ public function getItems(array $keys = [])
106112
return $result;
107113
}
108114

109-
public function clear()
115+
public function clear(array $tags = [])
110116
{
111-
$call = $this->timeCall(__FUNCTION__);
117+
$call = $this->timeCall(__FUNCTION__, [$tags]);
112118
$this->calls[] = $call;
113119

114120
return $call->result;
115121
}
116122

117-
public function deleteItems(array $keys)
123+
public function deleteItems(array $keys, array $tags = [])
118124
{
119-
$call = $this->timeCall(__FUNCTION__, [$keys]);
125+
$call = $this->timeCall(__FUNCTION__, [$keys, $tags]);
120126
$this->calls[] = $call;
121127

122128
return $call->result;

src/DataCollector/CacheDataCollector.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public function collect(Request $request, Response $response, \Exception $except
5858
$empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
5959
$this->data = ['instances' => $empty, 'total' => $empty];
6060
foreach ($this->instances as $name => $instance) {
61-
$calls = $instance->getCalls();
62-
$this->data['instances']['calls'][$name] = $calls;
61+
$this->data['instances']['calls'][$name] = $instance->getCalls();
6362
}
63+
6464
$this->data['instances']['statistics'] = $this->calculateStatistics();
6565
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
6666
}
@@ -126,19 +126,21 @@ private function calculateStatistics()
126126
foreach ($calls as $call) {
127127
$statistics[$name]['calls'] += 1;
128128
$statistics[$name]['time'] += $call->time;
129-
if ($call->name === 'fetch') {
129+
if ($call->name === 'getItem') {
130130
$statistics[$name]['reads'] += 1;
131131
if ($call->result !== false) {
132132
$statistics[$name]['hits'] += 1;
133133
} else {
134134
$statistics[$name]['misses'] += 1;
135135
}
136-
} elseif ($call->name === 'contains' && $call->result === false) {
136+
} elseif ($call->name === 'hasItem') {
137137
$statistics[$name]['reads'] += 1;
138-
$statistics[$name]['misses'] += 1;
138+
if ($call->result === false) {
139+
$statistics[$name]['misses'] += 1;
140+
}
139141
} elseif ($call->name === 'save') {
140142
$statistics[$name]['writes'] += 1;
141-
} elseif ($call->name === 'delete') {
143+
} elseif ($call->name === 'deleteItem') {
142144
$statistics[$name]['deletes'] += 1;
143145
}
144146
}

src/DependencyInjection/Compiler/DataCollectorCompilerPass.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@ protected function prepare()
3232

3333
foreach (array_keys($serviceIds) as $id) {
3434

35-
// Duplicating definition to $originalServiceId.logged
36-
$this->container->setDefinition($id.'.logged', $this->container->findDefinition($id));
37-
3835
// Creating a LoggingCachePool instance, and passing it the new definition from above
3936
$def = $this->container->register($id.'.logger', LoggingCachePool::class);
40-
$def->addArgument(new Reference($id.'.logged'));
37+
$def->addArgument(new Reference($id.'.logger.inner'))
38+
->setDecoratedService($id, null, 10);
4139

42-
// Overwrite the original service id with the new LoggingCachePool instance
43-
$this->container->setAlias($id, $id.'.logger');
40+
// Tell the collector to add the new logger
4441
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id.'.logger')]);
4542
}
4643
}

0 commit comments

Comments
 (0)