Skip to content

Commit

Permalink
use Swoole Table to store Redis data
Browse files Browse the repository at this point in the history
This fixes PHP warning message "Deprecated: Creation of dynamic property ... is deprecated"

Signed-off-by: Demin Yin <deminy@deminy.net>
  • Loading branch information
deminy committed Dec 15, 2023
1 parent 777f3e5 commit 2758639
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions examples/servers/redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@
*/

use Swoole\Redis\Server;
use Swoole\Table;

$server = new Server('0.0.0.0', 6379);
// We use a Swoole table as the data storage for the Redis server.
$table = new Table(1024);
$table->column('value', Table::TYPE_STRING, 64);
$table->create();

// We use an array as the data storage for the Redis server.
$server->data = []; // @phpstan-ignore property.notFound
$server = new Server('0.0.0.0', 6379);

$server->setHandler('SET', function (int $fd, array $data) use ($server) {
$server->data[$data[0]] = $data[1];
$server->setHandler('SET', function (int $fd, array $data) use ($server, $table) {
$table->set($data[0], ['value' => $data[1]]);
$server->send($fd, Server::format(Server::STATUS, 'OK'));
});

$server->setHandler('GET', function (int $fd, array $data) use ($server) {
$server->setHandler('GET', function (int $fd, array $data) use ($server, $table) {
$key = $data[0];
if (array_key_exists($key, $server->data)) {
$server->send($fd, Server::format(Server::STRING, $server->data[$key]));
if ($table->exist($key)) {
$server->send($fd, Server::format(Server::STRING, $table->get($key)['value'])); // @phpstan-ignore offsetAccess.nonOffsetAccessible
} else {
$server->send($fd, Server::format(Server::NIL));
}
Expand Down

0 comments on commit 2758639

Please sign in to comment.