Skip to content

Commit e81df7b

Browse files
author
Adam Halfar
committed
Add deflate and inflate of panel content, changed gc chance from 1% to 10%, added 10% chance to vacuum when sqlite is used, added pragma commands for optimalization when using sqlite
1 parent c0f4280 commit e81df7b

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/Model/Entity/Panel.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Panel extends Entity
3636
protected array $_hidden = ['content'];
3737

3838
/**
39-
* Read the stream contents.
39+
* Read the stream contents or inflate deflated data.
4040
*
4141
* Over certain sizes PDO will return file handles.
4242
* For backwards compatibility and consistency we smooth over that difference here.
@@ -46,10 +46,37 @@ class Panel extends Entity
4646
*/
4747
protected function _getContent(mixed $content): string
4848
{
49+
if (is_string($content)) {
50+
$contentInflated = @gzinflate($content);
51+
if ($contentInflated) {
52+
return $contentInflated;
53+
}
54+
55+
return $content;
56+
}
57+
4958
if (is_resource($content)) {
5059
return (string)stream_get_contents($content);
5160
}
5261

62+
return '';
63+
}
64+
65+
/**
66+
* Deflate the string data before saving it into database
67+
*
68+
* @param mixed $content Content
69+
* @return mixed
70+
*/
71+
protected function _setContent(mixed $content): mixed
72+
{
73+
if (is_string($content)) {
74+
$contentDeflated = gzdeflate($content, 9);
75+
if ($contentDeflated) {
76+
$content = $contentDeflated;
77+
}
78+
}
79+
5380
return $content;
5481
}
5582
}

src/Model/Table/RequestsTable.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,17 @@ public function findRecent(SelectQuery $query): SelectQuery
8686
*/
8787
protected function shouldGc(): bool
8888
{
89-
return rand(1, 100) === 100;
89+
return rand(1, 10) === 10;
90+
}
91+
92+
/**
93+
* Check if garbage collection vacuum should be run
94+
*
95+
* @return bool
96+
*/
97+
protected function shouldGcVacuum(): bool
98+
{
99+
return rand(1, 10) === 10;
90100
}
91101

92102
/**
@@ -131,7 +141,26 @@ public function gc(): void
131141

132142
$conn = $this->getConnection();
133143
if ($conn->getDriver() instanceof Sqlite) {
134-
$conn->execute('VACUUM;');
144+
$conn->execute('
145+
PRAGMA auto_vacuum = FULL;
146+
PRAGMA journal_mode = OFF;
147+
PRAGMA synchronous = OFF;
148+
PRAGMA foreign_keys = OFF;
149+
PRAGMA temp_store = MEMORY;
150+
PRAGMA automatic_index = OFF;
151+
');
152+
153+
if (!$this->shouldGcVacuum()) {
154+
return;
155+
}
156+
157+
try {
158+
$conn->execute('VACUUM;');
159+
} catch (PDOException) {
160+
if (is_file(TMP . 'debug_kit.sqlite')) {
161+
unlink(TMP . 'debug_kit.sqlite');
162+
}
163+
}
135164
}
136165
} catch (PDOException $e) {
137166
Log::warning('Unable to garbage collect requests table. This is probably due to concurrent requests.');

0 commit comments

Comments
 (0)