Added lifetime/cleanup support.
This commit is contained in:
parent
365e73aa71
commit
a0329c37c9
@ -14,6 +14,7 @@ namespace Symfony\Component\HttpKernel\Profiler;
|
|||||||
class MongoDbProfilerStorage implements ProfilerStorageInterface
|
class MongoDbProfilerStorage implements ProfilerStorageInterface
|
||||||
{
|
{
|
||||||
protected $dsn;
|
protected $dsn;
|
||||||
|
protected $lifetime;
|
||||||
private $mongo;
|
private $mongo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,9 +22,10 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
|||||||
*
|
*
|
||||||
* @param string $dsn A data source name
|
* @param string $dsn A data source name
|
||||||
*/
|
*/
|
||||||
public function __construct($dsn)
|
public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
|
||||||
{
|
{
|
||||||
$this->dsn = $dsn;
|
$this->dsn = $dsn;
|
||||||
|
$this->lifetime = (int) $lifetime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,6 +86,8 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
|||||||
*/
|
*/
|
||||||
public function write(Profile $profile)
|
public function write(Profile $profile)
|
||||||
{
|
{
|
||||||
|
$this->cleanup();
|
||||||
|
|
||||||
$record = array(
|
$record = array(
|
||||||
'_id' => $profile->getToken(),
|
'_id' => $profile->getToken(),
|
||||||
'parent' => $profile->getParent() ? $profile->getParent()->getToken() : null,
|
'parent' => $profile->getParent() ? $profile->getParent()->getToken() : null,
|
||||||
@ -153,6 +157,11 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
|
|||||||
return $profiles;
|
return $profiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function cleanup()
|
||||||
|
{
|
||||||
|
$this->getMongo()->remove(array('time' => array('$lt' => time() - $this->lifetime)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $ip
|
* @param string $ip
|
||||||
* @param string $url
|
* @param string $url
|
||||||
|
@ -26,7 +26,7 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase
|
|||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
if (extension_loaded('mongo')) {
|
if (extension_loaded('mongo')) {
|
||||||
self::$storage = new DummyMongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data');
|
self::$storage = new DummyMongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data', '', '', 86400);
|
||||||
try {
|
try {
|
||||||
self::$storage->getMongo();
|
self::$storage->getMongo();
|
||||||
} catch(\MongoConnectionException $e) {
|
} catch(\MongoConnectionException $e) {
|
||||||
@ -40,7 +40,7 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testStore()
|
public function testStore()
|
||||||
{
|
{
|
||||||
for ($i = 0; $i < 10; $i ++) {
|
for ($i = 0; $i < 10; $i++) {
|
||||||
$profile = new Profile('token_'.$i);
|
$profile = new Profile('token_'.$i);
|
||||||
$profile->setIp('127.0.0.1');
|
$profile->setIp('127.0.0.1');
|
||||||
$profile->setUrl('http://foo.bar');
|
$profile->setUrl('http://foo.bar');
|
||||||
@ -74,8 +74,8 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testStoreTime()
|
public function testStoreTime()
|
||||||
{
|
{
|
||||||
$dt = new \DateTime('2011-09-07 00:00:00');
|
$dt = new \DateTime('now');
|
||||||
for ($i = 0; $i < 3; $i ++) {
|
for ($i = 0; $i < 3; $i++) {
|
||||||
$dt->modify('+1 minute');
|
$dt->modify('+1 minute');
|
||||||
$profile = new Profile('time_'.$i);
|
$profile = new Profile('time_'.$i);
|
||||||
$profile->setIp('127.0.0.1');
|
$profile->setIp('127.0.0.1');
|
||||||
@ -143,11 +143,26 @@ class MongoDbProfilerStorageTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testRetrieveByEmptyUrlAndIp()
|
public function testRetrieveByEmptyUrlAndIp()
|
||||||
{
|
{
|
||||||
for ($i = 0; $i < 5; $i ++) {
|
for ($i = 0; $i < 5; $i++) {
|
||||||
$profile = new Profile('token_'.$i);
|
$profile = new Profile('token_'.$i);
|
||||||
self::$storage->write($profile);
|
self::$storage->write($profile);
|
||||||
}
|
}
|
||||||
$this->assertEquals(count(self::$storage->find('', '', 10)), 5, '->find() returns all previously added records');
|
$this->assertEquals(count(self::$storage->find('', '', 10)), 5, '->find() returns all previously added records');
|
||||||
self::$storage->purge();
|
self::$storage->purge();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCleanup()
|
||||||
|
{
|
||||||
|
$dt = new \DateTime('-2 day');
|
||||||
|
for ($i = 0; $i < 3; $i++) {
|
||||||
|
$dt->modify('-1 day');
|
||||||
|
$profile = new Profile('time_'.$i);
|
||||||
|
$profile->setTime($dt->getTimestamp());
|
||||||
|
self::$storage->write($profile);
|
||||||
|
}
|
||||||
|
$records = self::$storage->find('', '', 3);
|
||||||
|
$this->assertEquals(count($records), 1, '->find() returns only one record');
|
||||||
|
$this->assertEquals($records[0]['token'], 'time_2', '->find() returns the latest added record');
|
||||||
|
self::$storage->purge();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user