Skip to content

Commit ac32ab0

Browse files
committed
Make array repository a singleton
1 parent ec0dc13 commit ac32ab0

26 files changed

+88
-47
lines changed

src/ActiveRedisServiceProvider.php

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

33
namespace DirectoryTree\ActiveRedis;
44

5+
use DirectoryTree\ActiveRedis\Repositories\ArrayRepository;
56
use Illuminate\Support\ServiceProvider;
67

78
class ActiveRedisServiceProvider extends ServiceProvider
@@ -12,6 +13,8 @@ class ActiveRedisServiceProvider extends ServiceProvider
1213
public function register(): void
1314
{
1415
Model::clearBootedModels();
16+
17+
$this->app->singleton(ArrayRepository::class);
1518
}
1619

1720
/**

src/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ public static function setRepository(string $repository): void
432432
protected function newRepository(): Repository
433433
{
434434
return match ($repository = static::$repository) {
435-
'array' => new ArrayRepository,
436-
'redis' => new RedisRepository($this->getConnection()),
435+
'array' => app(ArrayRepository::class),
436+
'redis' => app(RedisRepository::class, ['redis' => $this->getConnection()]),
437437
default => throw new InvalidArgumentException("Repository [{$repository}] is not supported."),
438438
};
439439
}

src/Query.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Query
2323
*/
2424
public function __construct(
2525
protected Model $model,
26-
protected Repository $cache,
26+
protected Repository $repository,
2727
) {}
2828

2929
/**
@@ -39,7 +39,7 @@ public function find(?string $id, int $chunk = 1000): ?Model
3939
return $this->whereKey($id)->first($chunk);
4040
}
4141

42-
if (! $this->cache->exists($hash = $this->model->getBaseHashWithKey($id))) {
42+
if (! $this->repository->exists($hash = $this->model->getBaseHashWithKey($id))) {
4343
return null;
4444
}
4545

@@ -232,7 +232,7 @@ public function each(Closure $callback, int $chunk = 1000): void
232232
*/
233233
public function chunk(int $count, Closure $callback): void
234234
{
235-
foreach ($this->cache->chunk($this->getQuery(), $count) as $chunk) {
235+
foreach ($this->repository->chunk($this->getQuery(), $count) as $chunk) {
236236
$models = $this->model->newCollection();
237237

238238
foreach ($chunk as $hash) {
@@ -251,7 +251,7 @@ public function chunk(int $count, Closure $callback): void
251251
protected function newModelFromHash(string $hash): Model
252252
{
253253
return $this->model->newFromBuilder([
254-
...$this->cache->getAttributes($hash),
254+
...$this->repository->getAttributes($hash),
255255
$this->model->getKeyName() => $this->getKeyValue($hash),
256256
]);
257257
}
@@ -286,18 +286,34 @@ public function getQuery(): string
286286
return sprintf('%s:%s', $this->model->getHashPrefix(), rtrim($pattern, ':'));
287287
}
288288

289+
/**
290+
* Get the model instance.
291+
*/
292+
public function getModel(): Model
293+
{
294+
return $this->model;
295+
}
296+
297+
/**
298+
* Get the repository instance.
299+
*/
300+
public function getRepository(): Repository
301+
{
302+
return $this->repository;
303+
}
304+
289305
/**
290306
* Insert or update a record in the cache.
291307
*/
292308
public function insertOrUpdate(string $hash, array $attributes = []): void
293309
{
294-
$this->cache->transaction(function () use ($hash, $attributes) {
310+
$this->repository->transaction(function () use ($hash, $attributes) {
295311
if (! empty($delete = array_keys($attributes, null, true))) {
296-
$this->cache->deleteAttributes($hash, $delete);
312+
$this->repository->deleteAttributes($hash, $delete);
297313
}
298314

299315
if (! empty($update = array_diff_key($attributes, array_flip($delete)))) {
300-
$this->cache->setAttributes($hash, $update);
316+
$this->repository->setAttributes($hash, $update);
301317
}
302318
});
303319
}
@@ -307,22 +323,22 @@ public function insertOrUpdate(string $hash, array $attributes = []): void
307323
*/
308324
public function expire(string $hash, int $seconds): void
309325
{
310-
$this->cache->setExpiry($hash, $seconds);
326+
$this->repository->setExpiry($hash, $seconds);
311327
}
312328

313329
/**
314330
* Get a model's time-to-live (in seconds).
315331
*/
316332
public function expiry(string $hash): ?int
317333
{
318-
return $this->cache->getExpiry($hash);
334+
return $this->repository->getExpiry($hash);
319335
}
320336

321337
/**
322338
* Delete a model from the cache.
323339
*/
324340
public function destroy(string $hash): void
325341
{
326-
$this->cache->delete($hash);
342+
$this->repository->delete($hash);
327343
}
328344
}

tests/ModelArrayQueryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use DirectoryTree\ActiveRedis\Repositories\ArrayRepository;
4+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStub;
5+
6+
beforeAll(fn () => ModelStub::setRepository('array'));
7+
afterAll(fn () => ModelStub::setRepository('redis'));
8+
9+
it('can use array repository', function () {
10+
ModelStub::create();
11+
ModelStub::create();
12+
13+
$repository = ModelStub::query()->getRepository();
14+
15+
expect($repository)->toBeInstanceOf(ArrayRepository::class);
16+
17+
$results = iterator_to_array(
18+
$repository->chunk('*', 100)
19+
);
20+
21+
expect($results[0])->toHaveCount(2);
22+
});

tests/ModelCastTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use Carbon\Carbon;
44
use Carbon\CarbonImmutable;
5-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelEnumStub;
6-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCasts;
5+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelEnumStub;
6+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStubWithCasts;
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Facades\Date;
99
use Illuminate\Support\Facades\Redis;

tests/ModelEventTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithEventListeners;
3+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStubWithEventListeners;
44
use Illuminate\Support\Facades\Redis;
55

66
beforeEach(function () {

tests/ModelHiddenAttributesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithHiddenAttributes;
4-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithVisibleAttributes;
3+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStubWithHiddenAttributes;
4+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStubWithVisibleAttributes;
55

66
it('does not include hidden attributes when transformed into array', function () {
77
$model = new ModelStubWithHiddenAttributes([

tests/ModelObserverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelObserverStub;
4-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub;
3+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelObserverStub;
4+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStub;
55
use Illuminate\Support\Facades\Redis;
66

77
beforeEach(function () {

tests/ModelQueryTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
use DirectoryTree\ActiveRedis\Exceptions\ModelNotFoundException;
44
use DirectoryTree\ActiveRedis\Query;
5-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub;
6-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCustomKey;
7-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCustomPrefix;
5+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStub;
6+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStubWithCustomKey;
7+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStubWithCustomPrefix;
88
use Illuminate\Support\Facades\Date;
99
use Illuminate\Support\Facades\Redis;
1010

@@ -64,11 +64,11 @@
6464

6565
it('throws exception when finding by non-existent key', function () {
6666
ModelStub::findOrFail('invalid');
67-
})->throws(ModelNotFoundException::class, 'No query results for model [DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub] invalid');
67+
})->throws(ModelNotFoundException::class, 'No query results for model [DirectoryTree\ActiveRedis\Tests\Stubs\ModelStub] invalid');
6868

6969
it('throws exception when retrieving first of an empty query', function () {
7070
ModelStub::firstOrFail();
71-
})->throws(ModelNotFoundException::class, 'No query results for model [DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub].');
71+
})->throws(ModelNotFoundException::class, 'No query results for model [DirectoryTree\ActiveRedis\Tests\Stubs\ModelStub].');
7272

7373
it('can create query', function () {
7474
expect(ModelStub::query())->toBeInstanceOf(Query::class);

tests/ModelRoutableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub;
3+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStub;
44

55
it('is routable', function () {
66
$model = ModelStub::create();

0 commit comments

Comments
 (0)