Skip to content

Commit

Permalink
Recording pool fix (#65)
Browse files Browse the repository at this point in the history
* 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
Nyholm authored Aug 15, 2016
1 parent c3ec43d commit 7766048
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 170 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"phpunit/phpunit": "^5.1 || ^4.0",
"symfony/symfony": "^2.7 || ^3.0",
"cache/psr-6-doctrine-bridge": "^2.0",
"cache/array-adapter": "^0.4"
"cache/array-adapter": "^0.4",
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
},
"suggest": {
"cache/adapter-bundle": "To register PSR-6 compliant cache implementations as services.",
Expand Down
101 changes: 0 additions & 101 deletions src/Cache/LoggingCachePool.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -49,6 +67,8 @@ public function __construct(CacheItemPoolInterface $cachePool)
protected function addCall($call)
{
$this->calls[] = $call;

$this->writeLog($call);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
72 changes: 72 additions & 0 deletions src/Cache/Recording/Factory.php
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;
}
}
21 changes: 21 additions & 0 deletions src/Cache/Recording/HierarchyAndTaggablePool.php
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
{
}
21 changes: 21 additions & 0 deletions src/Cache/Recording/HierarchyPool.php
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
{
}
28 changes: 28 additions & 0 deletions src/Cache/Recording/TaggablePool.php
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;
}
}
Loading

0 comments on commit 7766048

Please sign in to comment.