Skip to content

Commit

Permalink
updaet some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 21, 2019
1 parent 5fe5188 commit f8ea7c3
Show file tree
Hide file tree
Showing 6 changed files with 631 additions and 12 deletions.
30 changes: 30 additions & 0 deletions example/bench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Swoft\Swlib\SwooleBench;

require dirname(__DIR__) . '/src/SwooleBench.php';

$swooleVersion = SWOOLE_VERSION;

echo <<<EOF
============================================================
Swoole Version {$swooleVersion}
============================================================
\n
EOF;

$bench = new SwooleBench([]);

if (!$bench->initFromCli()) {
if ($error = $bench->getError()) {
$bench->println('Prepare Error:', $error);
}

exit(0);
}

$bench->run();

if ($error = $bench->getError()) {
$bench->println('Run Error:', $error);
}
2 changes: 1 addition & 1 deletion src/AutoLoader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Swlib;
namespace Swoft\Swlib;

use Swoft\Helper\ComposerJSON;
use Swoft\SwoftComponent;
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Swlib\Swoole;
namespace Swoft\Swlib;

use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Response;
Expand Down
11 changes: 11 additions & 0 deletions src/MemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Swoft\Swlib;

/**
* Class MemFactory
*/
final class MemFactory
{

}
36 changes: 26 additions & 10 deletions src/MemTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Swlib\Swoole;
namespace Swoft\Swlib;

use InvalidArgumentException;
use RuntimeException;
Expand All @@ -25,6 +25,8 @@
*/
class MemTable
{
public const KEY_FIELD = '__key';

/**
* Swoole memory table instance
*
Expand Down Expand Up @@ -123,7 +125,7 @@ public function addColumn(string $name, int $type, int $size = 0): self
*/
public function create(): bool
{
if ($this->isCreate()) {
if ($this->isCreated()) {
throw new RuntimeException('Memory table have been created, cannot recreated');
}

Expand All @@ -136,6 +138,9 @@ public function create(): bool
$this->table->column($name, $type, $size);
}

// Append key column for storage key value.
$this->table->column(self::KEY_FIELD, Table::TYPE_STRING, 255);

// Create memory table
$result = $table->create();

Expand All @@ -159,10 +164,13 @@ public function create(): bool
*/
public function set(string $key, array $data): bool
{
if (!$this->isCreate()) {
if (!$this->isCreated()) {
throw new RuntimeException('Memory table have not been create');
}

// Append key column for storage key value.
$data[self::KEY_FIELD] = $key;

return $this->getTable()->set($key, $data);
}

Expand Down Expand Up @@ -249,14 +257,22 @@ public function count(): int
****************************************************************************/

/**
* flush table
* clear/flush table data
*/
// public function flush(): void
// {
// foreach ($this->table as $row) {
// $this->table->del($row['key']);
// }
// }
public function clear(): void
{
$this->flush();
}

/**
* clear/flush table data
*/
public function flush(): void
{
foreach ($this->table as $row) {
$this->table->del($row[self::KEY_FIELD]);
}
}

/**
* Restore data from dbFile
Expand Down
Loading

0 comments on commit f8ea7c3

Please sign in to comment.