-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
12 changed files
with
330 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,20 @@ | |
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\CacheBundle\Cache; | ||
namespace Cache\CacheBundle\Cache\Recording; | ||
|
||
use Cache\Taggable\TaggablePoolInterface; | ||
use Cache\Taggable\TaggablePSR6PoolAdapter; | ||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* A pool that logs and collects all your cache calls. | ||
* | ||
* @author Aaron Scherer <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class RecordingCachePool implements CacheItemPoolInterface, TaggablePoolInterface | ||
class CachePool implements CacheItemPoolInterface, TaggablePoolInterface | ||
{ | ||
/** | ||
* @type array | ||
|
@@ -31,14 +34,29 @@ class RecordingCachePool implements CacheItemPoolInterface, TaggablePoolInterfac | |
*/ | ||
private $cachePool; | ||
|
||
/** | ||
* @type LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @type string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @type string | ||
*/ | ||
private $level = 'info'; | ||
|
||
/** | ||
* LoggingCachePool constructor. | ||
* | ||
* @param CacheItemPoolInterface $cachePool | ||
*/ | ||
public function __construct(CacheItemPoolInterface $cachePool) | ||
{ | ||
$this->cachePool = TaggablePSR6PoolAdapter::makeTaggable($cachePool); | ||
$this->cachePool = $cachePool; | ||
} | ||
|
||
/** | ||
|
@@ -49,6 +67,8 @@ public function __construct(CacheItemPoolInterface $cachePool) | |
protected function addCall($call) | ||
{ | ||
$this->calls[] = $call; | ||
|
||
$this->writeLog($call); | ||
} | ||
|
||
/** | ||
|
@@ -57,7 +77,7 @@ protected function addCall($call) | |
* | ||
* @return object | ||
*/ | ||
private function timeCall($name, array $arguments = []) | ||
protected function timeCall($name, array $arguments = []) | ||
{ | ||
$start = microtime(true); | ||
$result = call_user_func_array([$this->cachePool, $name], $arguments); | ||
|
@@ -198,4 +218,61 @@ private function getValueRepresentation($value) | |
|
||
return $rep; | ||
} | ||
|
||
protected function writeLog($call) | ||
{ | ||
if (!$this->logger) { | ||
return; | ||
} | ||
|
||
$data = [ | ||
'name' => $this->name, | ||
'method' => $call->name, | ||
'arguments' => json_encode($call->arguments), | ||
'hit' => isset($call->isHit) ? $call->isHit ? 'True' : 'False' : 'Invalid', | ||
'time' => round($call->time * 1000, 2), | ||
'result' => $call->result, | ||
]; | ||
|
||
$this->logger->log( | ||
$this->level, | ||
sprintf('[Cache] Provider: %s. Method: %s(%s). Hit: %s. Time: %sms. Result: %s', | ||
$data['name'], | ||
$data['method'], | ||
$data['arguments'], | ||
$data['hit'], | ||
$data['time'], | ||
$data['result'] | ||
), | ||
$data | ||
); | ||
} | ||
|
||
/** | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function setLogger(LoggerInterface $logger = null) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $level | ||
*/ | ||
public function setLevel($level) | ||
{ | ||
$this->level = $level; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache\cache-bundle package. | ||
* | ||
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\CacheBundle\Cache\Recording; | ||
|
||
use Cache\Hierarchy\HierarchicalPoolInterface; | ||
use Cache\Taggable\TaggablePoolInterface; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Create a recording CachePool. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class Factory | ||
{ | ||
/** | ||
* @type int|string | ||
*/ | ||
private $level; | ||
|
||
/** | ||
* @type LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param LoggerInterface $logger | ||
* @param string|int $level | ||
*/ | ||
public function __construct(LoggerInterface $logger = null, $level = null) | ||
{ | ||
$this->level = $level; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Decorate a CachePool with a recorder. Make sure we use a recorder that implements the same functionality | ||
* as the underling pool. | ||
* | ||
* @param CacheItemPoolInterface $pool | ||
* | ||
* @return CachePool|HierarchyAndTaggablePool|HierarchyPool|TaggablePool | ||
*/ | ||
public function create($name, CacheItemPoolInterface $pool) | ||
{ | ||
if ($pool instanceof TaggablePoolInterface && $pool instanceof HierarchicalPoolInterface) { | ||
$recorder = new HierarchyAndTaggablePool($pool); | ||
} elseif ($pool instanceof TaggablePoolInterface) { | ||
$recorder = new TaggablePool($pool); | ||
} elseif ($pool instanceof HierarchicalPoolInterface) { | ||
$recorder = new HierarchyPool($pool); | ||
} else { | ||
$recorder = new CachePool($pool); | ||
} | ||
|
||
$recorder->setName($name); | ||
$recorder->setLevel($this->level); | ||
$recorder->setLogger($this->logger); | ||
|
||
return $recorder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache\cache-bundle package. | ||
* | ||
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\CacheBundle\Cache\Recording; | ||
|
||
use Cache\Hierarchy\HierarchicalPoolInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class HierarchyAndTaggablePool extends TaggablePool implements HierarchicalPoolInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache\cache-bundle package. | ||
* | ||
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\CacheBundle\Cache\Recording; | ||
|
||
use Cache\Hierarchy\HierarchicalPoolInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class HierarchyPool extends CachePool implements HierarchicalPoolInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache\cache-bundle package. | ||
* | ||
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\CacheBundle\Cache\Recording; | ||
|
||
use Cache\Taggable\TaggablePoolInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class TaggablePool extends CachePool implements TaggablePoolInterface | ||
{ | ||
public function clearTags(array $tags) | ||
{ | ||
$call = $this->timeCall(__FUNCTION__, [$tags]); | ||
$this->addCall($call); | ||
|
||
return $call->result; | ||
} | ||
} |
Oops, something went wrong.