Skip to content

[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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions src/Symfony/Component/Cache/Adapter/TraceableAdapter.php
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
Copy link
Contributor

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?

Copy link
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

{
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 src/Symfony/Component/Cache/Tests/Adapter/TraceableAdapterTest.php
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);
}
}