Skip to content

Commit 908c117

Browse files
authored
[1.x] Fix the bug of Swoole\Table using array access (#359)
* Fix the bug of Swoole\Table using array access * Remove ArrayObject
1 parent bfee55a commit 908c117

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
uses: shivammathur/setup-php@v2
2727
with:
2828
php-version: ${{ matrix.php }}
29-
extensions: dom, curl, libxml, mbstring, zip
29+
extensions: dom, curl, libxml, mbstring, zip, swoole
3030
tools: composer:v2
3131
coverage: none
3232

src/Cache/OctaneStore.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function __construct(protected $table)
3131
*/
3232
public function get($key)
3333
{
34-
$record = $this->table[$key] ?? null;
34+
$record = $this->table->get($key);
3535

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

@@ -79,12 +79,10 @@ public function many(array $keys)
7979
*/
8080
public function put($key, $value, $seconds)
8181
{
82-
$this->table[$key] = [
82+
return $this->table->set($key, [
8383
'value' => serialize($value),
8484
'expiration' => Carbon::now()->getTimestamp() + $seconds,
85-
];
86-
87-
return true;
85+
]);
8886
}
8987

9088
/**
@@ -112,9 +110,9 @@ public function putMany(array $values, $seconds)
112110
*/
113111
public function increment($key, $value = 1)
114112
{
115-
$record = $this->table[$key] ?? null;
113+
$record = $this->table->get($key);
116114

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

@@ -216,9 +214,7 @@ protected function intervalShouldBeRefreshed(array $interval)
216214
*/
217215
public function forget($key)
218216
{
219-
unset($this->table[$key]);
220-
221-
return true;
217+
return $this->table->del($key);
222218
}
223219

224220
/**
@@ -245,9 +241,9 @@ public function flush()
245241
* @param array|null $record
246242
* @return bool
247243
*/
248-
protected function recordIsNullOrExpired($record)
244+
protected function recordIsFalseOrExpired($record)
249245
{
250-
return is_null($record) || $record['expiration'] <= Carbon::now()->getTimestamp();
246+
return $record === false || $record['expiration'] <= Carbon::now()->getTimestamp();
251247
}
252248

253249
/**

tests/OctaneStoreTest.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Laravel\Octane\Tests;
44

5-
use ArrayObject;
65
use Illuminate\Support\Carbon;
76
use Illuminate\Support\Str;
87
use Laravel\Octane\Cache\OctaneStore;
@@ -11,9 +10,9 @@ class OctaneStoreTest extends TestCase
1110
{
1211
public function test_can_retrieve_items_from_store()
1312
{
14-
$table = new ArrayObject;
13+
$table = $this->createSwooleTable();
1514

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

1817
$store = new OctaneStore($table);
1918

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

2322
public function test_missing_items_return_null()
2423
{
25-
$table = new ArrayObject;
24+
$table = $this->createSwooleTable();
2625

2726
$store = new OctaneStore($table);
2827

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

3231
public function test_expired_items_return_null()
3332
{
34-
$table = new ArrayObject;
33+
$table = $this->createSwooleTable();
3534

3635
$store = new OctaneStore($table);
3736

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

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

4342
public function test_get_method_can_resolve_pending_interval()
4443
{
45-
$table = new ArrayObject;
44+
$table = $this->createSwooleTable();
4645

4746
$store = new OctaneStore($table);
4847

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

5453
public function test_many_method_can_return_many_values()
5554
{
56-
$table = new ArrayObject;
55+
$table = $this->createSwooleTable();
5756

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

6160
$store = new OctaneStore($table);
6261

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

6665
public function test_put_stores_value_in_table()
6766
{
68-
$table = new ArrayObject;
67+
$table = $this->createSwooleTable();
6968

7069
$store = new OctaneStore($table);
7170

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

7776
public function test_put_many_stores_value_in_table()
7877
{
79-
$table = new ArrayObject;
78+
$table = $this->createSwooleTable();
8079

8180
$store = new OctaneStore($table);
8281

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

8988
public function test_increment_and_decrement_operations()
9089
{
91-
$table = new ArrayObject;
90+
$table = $this->createSwooleTable();
9291

9392
$store = new OctaneStore($table);
9493

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

105104
public function test_forever_stores_value_in_table()
106105
{
107-
$table = new ArrayObject;
106+
$table = $this->createSwooleTable();
108107

109108
$store = new OctaneStore($table);
110109

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

116115
public function test_intervals_can_be_refreshed()
117116
{
118-
$table = new ArrayObject;
117+
$table = $this->createSwooleTable();
119118

120119
$store = new OctaneStore($table);
121120

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

136135
public function test_can_forget_cache_items()
137136
{
138-
$table = new ArrayObject;
137+
$table = $this->createSwooleTable();
139138

140139
$store = new OctaneStore($table);
141140

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

153152
public function test_intervals_are_not_flushed()
154153
{
155-
$table = new ArrayObject;
154+
$table = $this->createSwooleTable();
156155

157156
$store = new OctaneStore($table);
158157

tests/TestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Laravel\Octane\Testing\Fakes\FakeWorker;
1111
use Mockery;
1212
use PHPUnit\Framework\TestCase as BaseTestCase;
13+
use Swoole\Table;
1314

1415
class TestCase extends BaseTestCase
1516
{
@@ -40,6 +41,20 @@ protected function createApplication()
4041
return $app;
4142
}
4243

44+
protected function createSwooleTable()
45+
{
46+
$config = $this->config();
47+
48+
$cacheTable = new Table($config['cache']['rows'] ?? 1000);
49+
50+
$cacheTable->column('value', Table::TYPE_STRING, $config['cache']['bytes'] ?? 10000);
51+
$cacheTable->column('expiration', Table::TYPE_INT);
52+
53+
$cacheTable->create();
54+
55+
return $cacheTable;
56+
}
57+
4358
protected function appFactory()
4459
{
4560
return new ApplicationFactory(realpath(__DIR__.'/../vendor/orchestra/testbench-core/laravel'));

0 commit comments

Comments
 (0)