Skip to content

Commit beb1f55

Browse files
authored
Merge pull request #731 from cakephp/gc-fix
Revert "Use sub query instead of ids array."
2 parents 6b740c2 + c09f3c6 commit beb1f55

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/Model/Table/RequestsTable.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public function findRecent(Query $query, array $options)
7575
->limit(10);
7676
}
7777

78+
/**
79+
* Check if garbage collection should be run
80+
*
81+
* @return bool
82+
*/
83+
protected function shouldGc()
84+
{
85+
return time() % 100 === 0;
86+
}
87+
7888
/**
7989
* Garbage collect old request data.
8090
*
@@ -86,13 +96,16 @@ public function findRecent(Query $query, array $options)
8696
*/
8797
public function gc()
8898
{
89-
if (time() % 100 !== 0) {
99+
if (!$this->shouldGc()) {
90100
return;
91101
}
92102
$noPurge = $this->find()
93103
->select(['id'])
104+
->enableHydration(false)
94105
->order(['requested_at' => 'desc'])
95-
->limit(Configure::read('DebugKit.requestCount') ?: 20);
106+
->limit(Configure::read('DebugKit.requestCount') ?: 20)
107+
->extract('id')
108+
->toArray();
96109

97110
$query = $this->Panels->query()
98111
->delete()

tests/TestCase/Model/Table/RequestTableTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
namespace DebugKit\Test\TestCase\Model\Table;
1515

16+
use Cake\Core\Configure;
1617
use Cake\Database\Driver\Sqlite;
1718
use Cake\Datasource\ConnectionManager;
1819
use Cake\ORM\TableRegistry;
@@ -71,4 +72,35 @@ public function testFindRecent()
7172
$this->assertSame(10, $query->clause('limit'));
7273
$this->assertNotEmpty($query->clause('order'));
7374
}
75+
76+
/**
77+
* Test the garbage collect.
78+
*
79+
* @return void
80+
*/
81+
public function testGc()
82+
{
83+
/** @var \PHPUnit\Framework\MockObject\MockObject&\DebugKit\Model\Table\RequestsTable $requestsTableMock */
84+
$requestsTableMock = $this->getMockForModel('DebugKit.Requests', ['shouldGc']);
85+
$requestsTableMock->method('shouldGc')
86+
->will($this->returnValue(true));
87+
88+
$data = array_fill(0, 10, [
89+
'url' => '/tasks/add',
90+
'content_type' => 'text/html',
91+
'status_code' => 200,
92+
'requested_at' => '2014-08-21 7:41:12',
93+
]);
94+
$requests = $requestsTableMock->newEntities($data);
95+
$this->assertNotFalse($requestsTableMock->saveMany($requests));
96+
97+
$count = $requestsTableMock->find()->count();
98+
$this->assertGreaterThanOrEqual(10, $count);
99+
100+
Configure::write('DebugKit.requestCount', 5);
101+
$requestsTableMock->gc();
102+
103+
$count = $requestsTableMock->find()->count();
104+
$this->assertSame(5, $count);
105+
}
74106
}

0 commit comments

Comments
 (0)