Skip to content

Commit 7766048

Browse files
authored
Recording pool fix (#65)
* Make sure the recording pool does not change the type of pool. Also merge in logging in the recording pool * Style fixes * Added tests * style fix
1 parent c3ec43d commit 7766048

File tree

12 files changed

+330
-170
lines changed

12 files changed

+330
-170
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"phpunit/phpunit": "^5.1 || ^4.0",
3434
"symfony/symfony": "^2.7 || ^3.0",
3535
"cache/psr-6-doctrine-bridge": "^2.0",
36-
"cache/array-adapter": "^0.4"
36+
"cache/array-adapter": "^0.4",
37+
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
3738
},
3839
"suggest": {
3940
"cache/adapter-bundle": "To register PSR-6 compliant cache implementations as services.",

src/Cache/LoggingCachePool.php

Lines changed: 0 additions & 101 deletions
This file was deleted.

src/Cache/RecordingCachePool.php renamed to src/Cache/Recording/CachePool.php

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\CacheBundle\Cache;
12+
namespace Cache\CacheBundle\Cache\Recording;
1313

1414
use Cache\Taggable\TaggablePoolInterface;
15-
use Cache\Taggable\TaggablePSR6PoolAdapter;
1615
use Psr\Cache\CacheItemInterface;
1716
use Psr\Cache\CacheItemPoolInterface;
17+
use Psr\Log\LoggerInterface;
1818

1919
/**
20+
* A pool that logs and collects all your cache calls.
21+
*
2022
* @author Aaron Scherer <[email protected]>
23+
* @author Tobias Nyholm <[email protected]>
2124
*/
22-
class RecordingCachePool implements CacheItemPoolInterface, TaggablePoolInterface
25+
class CachePool implements CacheItemPoolInterface, TaggablePoolInterface
2326
{
2427
/**
2528
* @type array
@@ -31,14 +34,29 @@ class RecordingCachePool implements CacheItemPoolInterface, TaggablePoolInterfac
3134
*/
3235
private $cachePool;
3336

37+
/**
38+
* @type LoggerInterface
39+
*/
40+
private $logger;
41+
42+
/**
43+
* @type string
44+
*/
45+
private $name;
46+
47+
/**
48+
* @type string
49+
*/
50+
private $level = 'info';
51+
3452
/**
3553
* LoggingCachePool constructor.
3654
*
3755
* @param CacheItemPoolInterface $cachePool
3856
*/
3957
public function __construct(CacheItemPoolInterface $cachePool)
4058
{
41-
$this->cachePool = TaggablePSR6PoolAdapter::makeTaggable($cachePool);
59+
$this->cachePool = $cachePool;
4260
}
4361

4462
/**
@@ -49,6 +67,8 @@ public function __construct(CacheItemPoolInterface $cachePool)
4967
protected function addCall($call)
5068
{
5169
$this->calls[] = $call;
70+
71+
$this->writeLog($call);
5272
}
5373

5474
/**
@@ -57,7 +77,7 @@ protected function addCall($call)
5777
*
5878
* @return object
5979
*/
60-
private function timeCall($name, array $arguments = [])
80+
protected function timeCall($name, array $arguments = [])
6181
{
6282
$start = microtime(true);
6383
$result = call_user_func_array([$this->cachePool, $name], $arguments);
@@ -198,4 +218,61 @@ private function getValueRepresentation($value)
198218

199219
return $rep;
200220
}
221+
222+
protected function writeLog($call)
223+
{
224+
if (!$this->logger) {
225+
return;
226+
}
227+
228+
$data = [
229+
'name' => $this->name,
230+
'method' => $call->name,
231+
'arguments' => json_encode($call->arguments),
232+
'hit' => isset($call->isHit) ? $call->isHit ? 'True' : 'False' : 'Invalid',
233+
'time' => round($call->time * 1000, 2),
234+
'result' => $call->result,
235+
];
236+
237+
$this->logger->log(
238+
$this->level,
239+
sprintf('[Cache] Provider: %s. Method: %s(%s). Hit: %s. Time: %sms. Result: %s',
240+
$data['name'],
241+
$data['method'],
242+
$data['arguments'],
243+
$data['hit'],
244+
$data['time'],
245+
$data['result']
246+
),
247+
$data
248+
);
249+
}
250+
251+
/**
252+
* @param LoggerInterface $logger
253+
*/
254+
public function setLogger(LoggerInterface $logger = null)
255+
{
256+
$this->logger = $logger;
257+
}
258+
259+
/**
260+
* @param string $name
261+
*/
262+
public function setName($name)
263+
{
264+
$this->name = $name;
265+
266+
return $this;
267+
}
268+
269+
/**
270+
* @param string $level
271+
*/
272+
public function setLevel($level)
273+
{
274+
$this->level = $level;
275+
276+
return $this;
277+
}
201278
}

src/Cache/Recording/Factory.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-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+
namespace Cache\CacheBundle\Cache\Recording;
13+
14+
use Cache\Hierarchy\HierarchicalPoolInterface;
15+
use Cache\Taggable\TaggablePoolInterface;
16+
use Psr\Cache\CacheItemPoolInterface;
17+
use Psr\Log\LoggerInterface;
18+
19+
/**
20+
* Create a recording CachePool.
21+
*
22+
* @author Tobias Nyholm <[email protected]>
23+
*/
24+
class Factory
25+
{
26+
/**
27+
* @type int|string
28+
*/
29+
private $level;
30+
31+
/**
32+
* @type LoggerInterface
33+
*/
34+
private $logger;
35+
36+
/**
37+
* @param LoggerInterface $logger
38+
* @param string|int $level
39+
*/
40+
public function __construct(LoggerInterface $logger = null, $level = null)
41+
{
42+
$this->level = $level;
43+
$this->logger = $logger;
44+
}
45+
46+
/**
47+
* Decorate a CachePool with a recorder. Make sure we use a recorder that implements the same functionality
48+
* as the underling pool.
49+
*
50+
* @param CacheItemPoolInterface $pool
51+
*
52+
* @return CachePool|HierarchyAndTaggablePool|HierarchyPool|TaggablePool
53+
*/
54+
public function create($name, CacheItemPoolInterface $pool)
55+
{
56+
if ($pool instanceof TaggablePoolInterface && $pool instanceof HierarchicalPoolInterface) {
57+
$recorder = new HierarchyAndTaggablePool($pool);
58+
} elseif ($pool instanceof TaggablePoolInterface) {
59+
$recorder = new TaggablePool($pool);
60+
} elseif ($pool instanceof HierarchicalPoolInterface) {
61+
$recorder = new HierarchyPool($pool);
62+
} else {
63+
$recorder = new CachePool($pool);
64+
}
65+
66+
$recorder->setName($name);
67+
$recorder->setLevel($this->level);
68+
$recorder->setLogger($this->logger);
69+
70+
return $recorder;
71+
}
72+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-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+
namespace Cache\CacheBundle\Cache\Recording;
13+
14+
use Cache\Hierarchy\HierarchicalPoolInterface;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class HierarchyAndTaggablePool extends TaggablePool implements HierarchicalPoolInterface
20+
{
21+
}

src/Cache/Recording/HierarchyPool.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-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+
namespace Cache\CacheBundle\Cache\Recording;
13+
14+
use Cache\Hierarchy\HierarchicalPoolInterface;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class HierarchyPool extends CachePool implements HierarchicalPoolInterface
20+
{
21+
}

src/Cache/Recording/TaggablePool.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-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+
namespace Cache\CacheBundle\Cache\Recording;
13+
14+
use Cache\Taggable\TaggablePoolInterface;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class TaggablePool extends CachePool implements TaggablePoolInterface
20+
{
21+
public function clearTags(array $tags)
22+
{
23+
$call = $this->timeCall(__FUNCTION__, [$tags]);
24+
$this->addCall($call);
25+
26+
return $call->result;
27+
}
28+
}

0 commit comments

Comments
 (0)