Skip to content

Commit

Permalink
Merge pull request #1022 from Harfusha/5.next
Browse files Browse the repository at this point in the history
Add deflate and inflate of panel content, changed gc chance from 1% t…
  • Loading branch information
markstory authored Aug 29, 2024
2 parents c0f4280 + 2548003 commit acfbc05
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
31 changes: 29 additions & 2 deletions src/Model/Entity/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Panel extends Entity
protected array $_hidden = ['content'];

/**
* Read the stream contents.
* Read the stream contents or inflate deflated data.
*
* Over certain sizes PDO will return file handles.
* For backwards compatibility and consistency we smooth over that difference here.
Expand All @@ -47,7 +47,34 @@ class Panel extends Entity
protected function _getContent(mixed $content): string
{
if (is_resource($content)) {
return (string)stream_get_contents($content);
$content = (string)stream_get_contents($content);
}

if (is_string($content) && function_exists('gzinflate')) {
// phpcs:disable
$contentInflated = @gzinflate($content);
// phpcs:enable
if ($contentInflated !== false) {
return $contentInflated;
}
}

return $content;
}

/**
* Deflate the string data before saving it into database
*
* @param mixed $content Content
* @return mixed
*/
protected function _setContent(mixed $content): mixed
{
if (is_string($content) && function_exists('gzdeflate')) {
$contentDeflated = gzdeflate($content, 9);
if ($contentDeflated !== false) {
$content = $contentDeflated;
}
}

return $content;
Expand Down
34 changes: 32 additions & 2 deletions src/Model/Table/RequestsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace DebugKit\Model\Table;

use Cake\Core\Configure;
Expand Down Expand Up @@ -86,7 +87,17 @@ public function findRecent(SelectQuery $query): SelectQuery
*/
protected function shouldGc(): bool
{
return rand(1, 100) === 100;
return rand(1, 10) === 10;
}

/**
* Check if garbage collection vacuum should be run
*
* @return bool
*/
protected function shouldGcVacuum(): bool
{
return rand(1, 10) === 10;
}

/**
Expand Down Expand Up @@ -131,7 +142,26 @@ public function gc(): void

$conn = $this->getConnection();
if ($conn->getDriver() instanceof Sqlite) {
$conn->execute('VACUUM;');
$conn->execute('
PRAGMA auto_vacuum = FULL;
PRAGMA journal_mode = OFF;
PRAGMA synchronous = OFF;
PRAGMA foreign_keys = OFF;
PRAGMA temp_store = MEMORY;
PRAGMA automatic_index = OFF;
');

if (!$this->shouldGcVacuum()) {
return;
}

try {
$conn->execute('VACUUM;');
} catch (PDOException) {
// phpcs:disable
@unlink(TMP . 'debug_kit.sqlite');
// phpcs:enable
}
}
} catch (PDOException $e) {
Log::warning('Unable to garbage collect requests table. This is probably due to concurrent requests.');
Expand Down

0 comments on commit acfbc05

Please sign in to comment.