Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Fix the bug of Swoole\Table using array access #359

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
extensions: dom, curl, libxml, mbstring, zip, swoole
tools: composer:v2
coverage: none

Expand Down
22 changes: 9 additions & 13 deletions src/Cache/OctaneStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function __construct(protected $table)
*/
public function get($key)
{
$record = $this->table[$key] ?? null;
$record = $this->table->get($key);

if (! $this->recordIsNullOrExpired($record)) {
if (! $this->recordIsFalseOrExpired($record)) {
return unserialize($record['value']);
}

Expand Down Expand Up @@ -79,12 +79,10 @@ public function many(array $keys)
*/
public function put($key, $value, $seconds)
{
$this->table[$key] = [
return $this->table->set($key, [
'value' => serialize($value),
'expiration' => Carbon::now()->getTimestamp() + $seconds,
];

return true;
]);
}

/**
Expand Down Expand Up @@ -112,9 +110,9 @@ public function putMany(array $values, $seconds)
*/
public function increment($key, $value = 1)
{
$record = $this->table[$key] ?? null;
$record = $this->table->get($key);

if ($this->recordIsNullOrExpired($record)) {
if ($this->recordIsFalseOrExpired($record)) {
return tap($value, fn ($value) => $this->put($key, $value, static::ONE_YEAR));
}

Expand Down Expand Up @@ -216,9 +214,7 @@ protected function intervalShouldBeRefreshed(array $interval)
*/
public function forget($key)
{
unset($this->table[$key]);

return true;
return $this->table->del($key);
}

/**
Expand All @@ -245,9 +241,9 @@ public function flush()
* @param array|null $record
* @return bool
*/
protected function recordIsNullOrExpired($record)
protected function recordIsFalseOrExpired($record)
{
return is_null($record) || $record['expiration'] <= Carbon::now()->getTimestamp();
return $record === false || $record['expiration'] <= Carbon::now()->getTimestamp();
}

/**
Expand Down
33 changes: 16 additions & 17 deletions tests/OctaneStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Laravel\Octane\Tests;

use ArrayObject;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravel\Octane\Cache\OctaneStore;
Expand All @@ -11,9 +10,9 @@ class OctaneStoreTest extends TestCase
{
public function test_can_retrieve_items_from_store()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$table['foo'] = ['value' => serialize('bar'), 'expiration' => time() + 100];
$table->set('foo', ['value' => serialize('bar'), 'expiration' => time() + 100]);

$store = new OctaneStore($table);

Expand All @@ -22,7 +21,7 @@ public function test_can_retrieve_items_from_store()

public function test_missing_items_return_null()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -31,18 +30,18 @@ public function test_missing_items_return_null()

public function test_expired_items_return_null()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

$table['foo'] = ['value' => serialize('bar'), 'expiration' => time() - 100];
$table->set('foo', ['value' => serialize('bar'), 'expiration' => time() - 100]);

$this->assertNull($store->get('foo'));
}

public function test_get_method_can_resolve_pending_interval()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -53,10 +52,10 @@ public function test_get_method_can_resolve_pending_interval()

public function test_many_method_can_return_many_values()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$table['foo'] = ['value' => serialize('bar'), 'expiration' => time() + 100];
$table['bar'] = ['value' => serialize('baz'), 'expiration' => time() + 100];
$table->set('foo', ['value' => serialize('bar'), 'expiration' => time() + 100]);
$table->set('bar', ['value' => serialize('baz'), 'expiration' => time() + 100]);

$store = new OctaneStore($table);

Expand All @@ -65,7 +64,7 @@ public function test_many_method_can_return_many_values()

public function test_put_stores_value_in_table()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -76,7 +75,7 @@ public function test_put_stores_value_in_table()

public function test_put_many_stores_value_in_table()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -88,7 +87,7 @@ public function test_put_many_stores_value_in_table()

public function test_increment_and_decrement_operations()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -104,7 +103,7 @@ public function test_increment_and_decrement_operations()

public function test_forever_stores_value_in_table()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -115,7 +114,7 @@ public function test_forever_stores_value_in_table()

public function test_intervals_can_be_refreshed()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -135,7 +134,7 @@ public function test_intervals_can_be_refreshed()

public function test_can_forget_cache_items()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand All @@ -152,7 +151,7 @@ public function test_can_forget_cache_items()

public function test_intervals_are_not_flushed()
{
$table = new ArrayObject;
$table = $this->createSwooleTable();

$store = new OctaneStore($table);

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Laravel\Octane\Testing\Fakes\FakeWorker;
use Mockery;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Swoole\Table;

class TestCase extends BaseTestCase
{
Expand Down Expand Up @@ -40,6 +41,20 @@ protected function createApplication()
return $app;
}

protected function createSwooleTable()
{
$config = $this->config();

$cacheTable = new Table($config['cache']['rows'] ?? 1000);

$cacheTable->column('value', Table::TYPE_STRING, $config['cache']['bytes'] ?? 10000);
$cacheTable->column('expiration', Table::TYPE_INT);

$cacheTable->create();

return $cacheTable;
}

protected function appFactory()
{
return new ApplicationFactory(realpath(__DIR__.'/../vendor/orchestra/testbench-core/laravel'));
Expand Down