From 6676ba8014b8891b1725de0b08bc7bbfedddb547 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Fri, 6 Aug 2021 14:00:21 +0800 Subject: [PATCH 1/2] Fix the bug of Swoole\Table using array access --- .github/workflows/tests.yml | 2 +- src/Cache/OctaneStore.php | 22 +++++++++------------- tests/OctaneStoreTest.php | 32 ++++++++++++++++---------------- tests/TestCase.php | 15 +++++++++++++++ 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8d8cf745c..c58e1f240 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/src/Cache/OctaneStore.php b/src/Cache/OctaneStore.php index cffdfbab8..6156209e8 100644 --- a/src/Cache/OctaneStore.php +++ b/src/Cache/OctaneStore.php @@ -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']); } @@ -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; + ]); } /** @@ -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)); } @@ -216,9 +214,7 @@ protected function intervalShouldBeRefreshed(array $interval) */ public function forget($key) { - unset($this->table[$key]); - - return true; + return $this->table->del($key); } /** @@ -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(); } /** diff --git a/tests/OctaneStoreTest.php b/tests/OctaneStoreTest.php index 1edff706e..f30a5fd15 100644 --- a/tests/OctaneStoreTest.php +++ b/tests/OctaneStoreTest.php @@ -11,9 +11,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); @@ -22,7 +22,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); @@ -31,18 +31,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); @@ -53,10 +53,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); @@ -65,7 +65,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); @@ -76,7 +76,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); @@ -88,7 +88,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); @@ -104,7 +104,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); @@ -115,7 +115,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); @@ -135,7 +135,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); @@ -152,7 +152,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); diff --git a/tests/TestCase.php b/tests/TestCase.php index 3798b79bb..ead037474 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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 { @@ -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')); From 21862c13738ff0243c24261dd675803970b71acf Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Fri, 6 Aug 2021 14:06:00 +0800 Subject: [PATCH 2/2] Remove ArrayObject --- tests/OctaneStoreTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/OctaneStoreTest.php b/tests/OctaneStoreTest.php index f30a5fd15..3cbe06d96 100644 --- a/tests/OctaneStoreTest.php +++ b/tests/OctaneStoreTest.php @@ -2,7 +2,6 @@ namespace Laravel\Octane\Tests; -use ArrayObject; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Laravel\Octane\Cache\OctaneStore;