-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] TraceableAdapter #21082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[Cache] TraceableAdapter #21082
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1714aa6
Added traceable adapter
Nyholm 7ba9b1c
Bugfix
Nyholm 2f86f67
Using filesystem cache
Nyholm 1cac255
Remove hasItem code
Nyholm 19c401d
Updated tests
Nyholm 457c8f7
Update TraceableAdapter.php
Nyholm be9726a
Reset $calls after getCalls()
Nyholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
201 changes: 201 additions & 0 deletions
201
src/Symfony/Component/Cache/Adapter/TraceableAdapter.php
This file contains hidden or 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,201 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Adapter; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* An adapter that collects data about all cache calls. | ||
* | ||
* @author Aaron Scherer <aequasi@gmail.com> | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class TraceableAdapter implements AdapterInterface | ||
{ | ||
private $pool; | ||
private $calls = array(); | ||
|
||
public function __construct(AdapterInterface $pool) | ||
{ | ||
$this->pool = $pool; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem($key) | ||
{ | ||
$event = $this->start(__FUNCTION__, $key); | ||
try { | ||
$item = $this->pool->getItem($key); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
if ($item->isHit()) { | ||
++$event->hits; | ||
} else { | ||
++$event->misses; | ||
} | ||
$event->result = $item->get(); | ||
|
||
return $item; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasItem($key) | ||
{ | ||
$event = $this->start(__FUNCTION__, $key); | ||
try { | ||
return $event->result = $this->pool->hasItem($key); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItem($key) | ||
{ | ||
$event = $this->start(__FUNCTION__, $key); | ||
try { | ||
return $event->result = $this->pool->deleteItem($key); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(CacheItemInterface $item) | ||
{ | ||
$event = $this->start(__FUNCTION__, $item); | ||
try { | ||
return $event->result = $this->pool->save($item); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function saveDeferred(CacheItemInterface $item) | ||
{ | ||
$event = $this->start(__FUNCTION__, $item); | ||
try { | ||
return $event->result = $this->pool->saveDeferred($item); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems(array $keys = array()) | ||
{ | ||
$event = $this->start(__FUNCTION__, $keys); | ||
try { | ||
$result = $this->pool->getItems($keys); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
$f = function () use ($result, $event) { | ||
$event->result = array(); | ||
foreach ($result as $key => $item) { | ||
if ($item->isHit()) { | ||
++$event->hits; | ||
} else { | ||
++$event->misses; | ||
} | ||
$event->result[$key] = $item->get(); | ||
yield $key => $item; | ||
} | ||
}; | ||
|
||
return $f(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
$event = $this->start(__FUNCTION__); | ||
try { | ||
return $event->result = $this->pool->clear(); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItems(array $keys) | ||
{ | ||
$event = $this->start(__FUNCTION__, $keys); | ||
try { | ||
return $event->result = $this->pool->deleteItems($keys); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit() | ||
{ | ||
$event = $this->start(__FUNCTION__); | ||
try { | ||
return $event->result = $this->pool->commit(); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
public function getCalls() | ||
{ | ||
try { | ||
return $this->calls; | ||
} finally { | ||
$this->calls = array(); | ||
} | ||
} | ||
|
||
private function start($name, $argument = null) | ||
{ | ||
$this->calls[] = $event = new TraceableAdapterEvent(); | ||
$event->name = $name; | ||
$event->argument = $argument; | ||
$event->start = microtime(true); | ||
|
||
return $event; | ||
} | ||
} | ||
|
||
class TraceableAdapterEvent | ||
{ | ||
public $name; | ||
public $argument; | ||
public $start; | ||
public $end; | ||
public $result; | ||
public $hits = 0; | ||
public $misses = 0; | ||
} |
190 changes: 190 additions & 0 deletions
190
src/Symfony/Component/Cache/Tests/Adapter/TraceableAdapterTest.php
This file contains hidden or 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,190 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Tests\Adapter; | ||
|
||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
use Symfony\Component\Cache\Adapter\TraceableAdapter; | ||
|
||
/** | ||
* @group time-sensitive | ||
*/ | ||
class TraceableAdapterTest extends AdapterTestCase | ||
{ | ||
public function createCachePool($defaultLifetime = 0) | ||
{ | ||
return new TraceableAdapter(new FilesystemAdapter('', $defaultLifetime)); | ||
} | ||
|
||
public function testGetItemMiss() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$pool->getItem('k'); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(1, $calls); | ||
|
||
$call = $calls[0]; | ||
$this->assertEquals('getItem', $call->name); | ||
$this->assertEquals('k', $call->argument); | ||
$this->assertEquals(0, $call->hits); | ||
$this->assertEquals(1, $call->misses); | ||
$this->assertNull($call->result); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testGetItemHit() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$item = $pool->getItem('k')->set('foo'); | ||
$pool->save($item); | ||
$pool->getItem('k'); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(3, $calls); | ||
|
||
$call = $calls[2]; | ||
$this->assertEquals(1, $call->hits); | ||
$this->assertEquals(0, $call->misses); | ||
} | ||
|
||
public function testGetItemsMiss() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$arg = array('k0', 'k1'); | ||
$items = $pool->getItems($arg); | ||
foreach ($items as $item) { | ||
} | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(1, $calls); | ||
|
||
$call = $calls[0]; | ||
$this->assertEquals('getItems', $call->name); | ||
$this->assertEquals($arg, $call->argument); | ||
$this->assertEquals(2, $call->misses); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testHasItemMiss() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$pool->hasItem('k'); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(1, $calls); | ||
|
||
$call = $calls[0]; | ||
$this->assertEquals('hasItem', $call->name); | ||
$this->assertEquals('k', $call->argument); | ||
$this->assertFalse($call->result); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testHasItemHit() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$item = $pool->getItem('k')->set('foo'); | ||
$pool->save($item); | ||
$pool->hasItem('k'); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(3, $calls); | ||
|
||
$call = $calls[2]; | ||
$this->assertEquals('hasItem', $call->name); | ||
$this->assertEquals('k', $call->argument); | ||
$this->assertTrue($call->result); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testDeleteItem() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$pool->deleteItem('k'); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(1, $calls); | ||
|
||
$call = $calls[0]; | ||
$this->assertEquals('deleteItem', $call->name); | ||
$this->assertEquals('k', $call->argument); | ||
$this->assertEquals(0, $call->hits); | ||
$this->assertEquals(0, $call->misses); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testDeleteItems() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$arg = array('k0', 'k1'); | ||
$pool->deleteItems($arg); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(1, $calls); | ||
|
||
$call = $calls[0]; | ||
$this->assertEquals('deleteItems', $call->name); | ||
$this->assertEquals($arg, $call->argument); | ||
$this->assertEquals(0, $call->hits); | ||
$this->assertEquals(0, $call->misses); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testSave() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$item = $pool->getItem('k')->set('foo'); | ||
$pool->save($item); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(2, $calls); | ||
|
||
$call = $calls[1]; | ||
$this->assertEquals('save', $call->name); | ||
$this->assertEquals($item, $call->argument); | ||
$this->assertEquals(0, $call->hits); | ||
$this->assertEquals(0, $call->misses); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testSaveDeferred() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$item = $pool->getItem('k')->set('foo'); | ||
$pool->saveDeferred($item); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(2, $calls); | ||
|
||
$call = $calls[1]; | ||
$this->assertEquals('saveDeferred', $call->name); | ||
$this->assertEquals($item, $call->argument); | ||
$this->assertEquals(0, $call->hits); | ||
$this->assertEquals(0, $call->misses); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
|
||
public function testCommit() | ||
{ | ||
$pool = $this->createCachePool(); | ||
$pool->commit(); | ||
$calls = $pool->getCalls(); | ||
$this->assertCount(1, $calls); | ||
|
||
$call = $calls[0]; | ||
$this->assertEquals('commit', $call->name); | ||
$this->assertNull(null, $call->argument); | ||
$this->assertEquals(0, $call->hits); | ||
$this->assertEquals(0, $call->misses); | ||
$this->assertNotEmpty($call->start); | ||
$this->assertNotEmpty($call->end); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isnt it more a wrapper then a adapter as the class doesnt adapt one api for another?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly seen you are right. But I think it would be weird to change the name as we are implementing the
AdapterInterface
here, wouldn't it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see