Skip to content

Commit 68e4d14

Browse files
optimize code (#9335)
1 parent 17c1c8d commit 68e4d14

File tree

5 files changed

+8
-29
lines changed

5 files changed

+8
-29
lines changed

frameworks/PHP/swoole/database.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Operation
1515
public static function db(PDOStatement|PDOStatementProxy $db): string
1616
{
1717
$db->execute([mt_rand(1, 10000)]);
18-
return json_encode($db->fetch(PDO::FETCH_ASSOC), JSON_NUMERIC_CHECK);
18+
return json_encode($db->fetch(PDO::FETCH_ASSOC));
1919
}
2020

2121
public static function fortunes(PDOStatement|PDOStatementProxy $fortune): string
@@ -42,7 +42,7 @@ public static function query(PDOStatement|PDOStatementProxy $query, int $queries
4242
$results[] = $query->fetch(PDO::FETCH_ASSOC);
4343
}
4444

45-
return json_encode($results, JSON_NUMERIC_CHECK);
45+
return json_encode($results);
4646
}
4747

4848
public static function updates(PDOStatement|PDOStatementProxy $random, PDOStatement|PDOStatementProxy $update, int $queries, string $driver): string
@@ -65,7 +65,7 @@ public static function updates(PDOStatement|PDOStatementProxy $random, PDOStatem
6565
if ($driver == 'pgsql') {
6666
$update->execute([...$values, ...$keys]);
6767
}
68-
return json_encode($results, JSON_NUMERIC_CHECK);
68+
return json_encode($results);
6969
}
7070
}
7171

@@ -130,9 +130,6 @@ class Connections
130130
{
131131
private static PDOPool $pool;
132132
private static string $driver;
133-
private static array $dbs = [];
134-
private static array $fortunes = [];
135-
private static array $updates = [];
136133

137134
public static function init(string $driver): void
138135
{
@@ -195,28 +192,14 @@ private static function put(PDO|PDOProxy $db): void
195192

196193
private static function getStatement(PDO|PDOProxy $pdo, string $type, int $queries = 0): PDOStatement|PDOStatementProxy
197194
{
198-
$hash = spl_object_id($pdo);
199-
200195
if ('select' == $type) {
201-
if (!isset(self::$dbs[$hash])) {
202-
self::$dbs[$hash] = $pdo->prepare(Operation::WORLD_SELECT_SQL);
203-
}
204-
205-
return self::$dbs[$hash];
196+
return $pdo->prepare(Operation::WORLD_SELECT_SQL);
206197
} elseif ('fortunes' == $type) {
207-
if (!isset(self::$fortunes[$hash])) {
208-
self::$fortunes[$hash] = $pdo->prepare(Operation::FORTUNE_SQL);
209-
}
210-
211-
return self::$fortunes[$hash];
198+
return $pdo->prepare(Operation::FORTUNE_SQL);
212199
} else {
213-
if (!isset(self::$updates[$hash][$queries])) {
214-
self::$updates[$hash][$queries] = self::$driver == 'pgsql'
215-
? $pdo->prepare('UPDATE World SET randomNumber = CASE id'.\str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $queries).'END WHERE id IN ('.\str_repeat('?::INTEGER,', $queries - 1).'?::INTEGER)')
216-
: $pdo->prepare(Operation::WORLD_UPDATE_SQL);
217-
}
218-
219-
return self::$updates[$hash][$queries];
200+
return self::$driver == 'pgsql'
201+
? $pdo->prepare('UPDATE World SET randomNumber = CASE id'.\str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $queries).'END WHERE id IN ('.\str_repeat('?::INTEGER,', $queries - 1).'?::INTEGER)')
202+
: $pdo->prepare(Operation::WORLD_UPDATE_SQL);
220203
}
221204
}
222205
}

frameworks/PHP/swoole/swoole-async-mysql.dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ RUN apt update -yqq > /dev/null \
2525
WORKDIR /swoole
2626

2727
ADD ./swoole-server.php /swoole
28-
ADD 10-opcache.ini /swoole
2928
ADD ./database.php /swoole
3029

3130
COPY 10-opcache.ini /etc/php/8.3/cli/conf.d/10-opcache.ini

frameworks/PHP/swoole/swoole-async-postgres.dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ RUN apt update -yqq > /dev/null \
2525
WORKDIR /swoole
2626

2727
ADD ./swoole-server.php /swoole
28-
ADD 10-opcache.ini /swoole
2928
ADD ./database.php /swoole
3029

3130
COPY 10-opcache.ini /etc/php/8.3/cli/conf.d/10-opcache.ini

frameworks/PHP/swoole/swoole-sync-mysql.dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ RUN apt update -yqq > /dev/null \
2525
WORKDIR /swoole
2626

2727
ADD ./swoole-server.php /swoole
28-
ADD 10-opcache.ini /swoole
2928
ADD ./database.php /swoole
3029

3130
COPY 10-opcache.ini /etc/php/8.3/cli/conf.d/10-opcache.ini

frameworks/PHP/swoole/swoole-sync-postgres.dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ RUN apt update -yqq > /dev/null \
2525
WORKDIR /swoole
2626

2727
ADD ./swoole-server.php /swoole
28-
ADD 10-opcache.ini /swoole
2928
ADD ./database.php /swoole
3029

3130
COPY 10-opcache.ini /etc/php/8.3/cli/conf.d/10-opcache.ini

0 commit comments

Comments
 (0)