Skip to content

Commit 64c55f3

Browse files
committed
removed unnecessary key generation & add StorageServiceLockDecorator
1 parent fda174c commit 64c55f3

File tree

8 files changed

+85
-40
lines changed

8 files changed

+85
-40
lines changed

deptrac.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ parameters:
9898
- type: className
9999
regex: ^Bavix\\.*Internal\\Service\\.*Service$
100100

101+
- name: ServiceDecorator
102+
collectors:
103+
- type: className
104+
regex: ^Bavix\\.*Decorator\\.*Decorator
105+
101106
# contracts
102107
- name: Model
103108
collectors:
@@ -162,6 +167,10 @@ parameters:
162167
- Config
163168
- Cache
164169

170+
ServiceDecorator:
171+
- InternalException
172+
- ServiceInterface
173+
165174
EventInterface:
166175
Event:
167176
- EventInterface

docs/transaction.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Transaction
22

3+
> Since version 9.2 it is safer to use AtomicServiceInterface.
4+
35
> Is it possible to use laravel's inline transactions? No, It is Immpossible. This limitation is due to the internal architecture of the package. To achieve the maximum speed of work, work with an internal state of balance was needed. Starting with version 8.2, a special error has appeared that will inform you about incorrect work with the `TransactionStartException` package.
46
57
Sometimes you need to execute many simple queries. You want to keep the data atomic. To do this, you need `laravel-wallet` v7.1+.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Decorator;
6+
7+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
8+
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
9+
use Bavix\Wallet\Internal\Service\LockServiceInterface;
10+
use Bavix\Wallet\Internal\Service\StorageServiceInterface;
11+
12+
final class StorageServiceLockDecorator implements StorageServiceInterface
13+
{
14+
public function __construct(
15+
private StorageServiceInterface $storageService,
16+
private LockServiceInterface $lockService
17+
) {
18+
}
19+
20+
public function flush(): bool
21+
{
22+
return $this->storageService->flush();
23+
}
24+
25+
public function missing(string $uuid): bool
26+
{
27+
return $this->storageService->missing($uuid);
28+
}
29+
30+
public function get(string $uuid): string
31+
{
32+
return $this->storageService->get($uuid);
33+
}
34+
35+
public function sync(string $uuid, float|int|string $value): bool
36+
{
37+
return $this->storageService->sync($uuid, $value);
38+
}
39+
40+
/**
41+
* @throws LockProviderNotFoundException
42+
* @throws RecordNotFoundException
43+
*/
44+
public function increase(string $uuid, float|int|string $value): string
45+
{
46+
return $this->lockService->block(
47+
$uuid . '::increase',
48+
fn () => $this->storageService->increase($uuid, $value)
49+
);
50+
}
51+
}

src/Internal/Service/StorageService.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
namespace Bavix\Wallet\Internal\Service;
66

77
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
8-
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
98
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
109
use Illuminate\Contracts\Cache\Repository as CacheRepository;
1110

1211
final class StorageService implements StorageServiceInterface
1312
{
1413
public function __construct(
15-
private LockServiceInterface $lockService,
1614
private MathServiceInterface $mathService,
1715
private CacheRepository $cacheRepository
1816
) {
@@ -23,17 +21,17 @@ public function flush(): bool
2321
return $this->cacheRepository->clear();
2422
}
2523

26-
public function missing(string $key): bool
24+
public function missing(string $uuid): bool
2725
{
28-
return $this->cacheRepository->forget($key);
26+
return $this->cacheRepository->forget($uuid);
2927
}
3028

3129
/**
3230
* @throws RecordNotFoundException
3331
*/
34-
public function get(string $key): string
32+
public function get(string $uuid): string
3533
{
36-
$value = $this->cacheRepository->get($key);
34+
$value = $this->cacheRepository->get($uuid);
3735
if ($value === null) {
3836
throw new RecordNotFoundException(
3937
'The repository did not find the object',
@@ -44,25 +42,19 @@ public function get(string $key): string
4442
return $this->mathService->round($value);
4543
}
4644

47-
public function sync(string $key, float|int|string $value): bool
45+
public function sync(string $uuid, float|int|string $value): bool
4846
{
49-
return $this->cacheRepository->set($key, $value);
47+
return $this->cacheRepository->set($uuid, $value);
5048
}
5149

5250
/**
53-
* @throws LockProviderNotFoundException
5451
* @throws RecordNotFoundException
5552
*/
56-
public function increase(string $key, float|int|string $value): string
53+
public function increase(string $uuid, float|int|string $value): string
5754
{
58-
return $this->lockService->block(
59-
$key . '::increase',
60-
function () use ($key, $value): string {
61-
$result = $this->mathService->add($this->get($key), $value);
62-
$this->sync($key, $result);
55+
$result = $this->mathService->add($this->get($uuid), $value);
56+
$this->sync($uuid, $result);
6357

64-
return $this->mathService->round($result);
65-
}
66-
);
58+
return $this->mathService->round($result);
6759
}
6860
}

src/Internal/Service/StorageServiceInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ interface StorageServiceInterface
1111
{
1212
public function flush(): bool;
1313

14-
public function missing(string $key): bool;
14+
public function missing(string $uuid): bool;
1515

1616
/**
1717
* @throws RecordNotFoundException
1818
*/
19-
public function get(string $key): string;
19+
public function get(string $uuid): string;
2020

21-
public function sync(string $key, float|int|string $value): bool;
21+
public function sync(string $uuid, float|int|string $value): bool;
2222

2323
/**
2424
* @throws LockProviderNotFoundException
2525
* @throws RecordNotFoundException
2626
*/
27-
public function increase(string $key, float|int|string $value): string;
27+
public function increase(string $uuid, float|int|string $value): string;
2828
}

src/Services/BookkeeperService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ public function increase(Wallet $wallet, float|int|string $value): string
6666

6767
private function getKey(Wallet $wallet): string
6868
{
69-
return __CLASS__ . '::' . $wallet->uuid;
69+
return $wallet->uuid;
7070
}
7171
}

src/Services/RegulatorService.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,38 @@
99
use Bavix\Wallet\Internal\Service\DispatcherServiceInterface;
1010
use Bavix\Wallet\Internal\Service\MathServiceInterface;
1111
use Bavix\Wallet\Internal\Service\StorageServiceInterface;
12-
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
1312
use Bavix\Wallet\Models\Wallet;
1413

1514
/**
1615
* @internal
1716
*/
1817
final class RegulatorService implements RegulatorServiceInterface
1918
{
20-
private string $idempotentKey;
21-
2219
/**
2320
* @var Wallet[]
2421
*/
2522
private array $wallets = [];
2623

2724
public function __construct(
2825
private BalanceUpdatedEventAssemblerInterface $balanceUpdatedEventAssembler,
29-
UuidFactoryServiceInterface $uuidFactoryService,
3026
private BookkeeperServiceInterface $bookkeeperService,
3127
private DispatcherServiceInterface $dispatcherService,
3228
private StorageServiceInterface $storageService,
3329
private MathServiceInterface $mathService
3430
) {
35-
$this->idempotentKey = $uuidFactoryService->uuid4();
3631
}
3732

3833
public function missing(Wallet $wallet): bool
3934
{
4035
unset($this->wallets[$wallet->uuid]);
4136

42-
return $this->storageService->missing($this->getKey($wallet->uuid));
37+
return $this->storageService->missing($wallet->uuid);
4338
}
4439

4540
public function diff(Wallet $wallet): string
4641
{
4742
try {
48-
return $this->mathService->round($this->storageService->get($this->getKey($wallet->uuid)));
43+
return $this->mathService->round($this->storageService->get($wallet->uuid));
4944
} catch (RecordNotFoundException) {
5045
return '0';
5146
}
@@ -63,7 +58,7 @@ public function sync(Wallet $wallet, float|int|string $value): bool
6358
$this->persist($wallet);
6459

6560
return $this->storageService->sync(
66-
$this->getKey($wallet->uuid),
61+
$wallet->uuid,
6762
$this->mathService->round(
6863
$this->mathService->negative($this->mathService->sub($this->amount($wallet), $value))
6964
)
@@ -75,10 +70,10 @@ public function increase(Wallet $wallet, float|int|string $value): string
7570
$this->persist($wallet);
7671

7772
try {
78-
$this->storageService->increase($this->getKey($wallet->uuid), $value);
73+
$this->storageService->increase($wallet->uuid, $value);
7974
} catch (RecordNotFoundException) {
8075
$value = $this->mathService->round($value);
81-
$this->storageService->sync($this->getKey($wallet->uuid), $value);
76+
$this->storageService->sync($wallet->uuid, $value);
8277
}
8378

8479
return $this->amount($wallet);
@@ -129,9 +124,4 @@ private function persist(Wallet $wallet): void
129124
{
130125
$this->wallets[$wallet->uuid] = $wallet;
131126
}
132-
133-
private function getKey(string $uuid): string
134-
{
135-
return $this->idempotentKey . '::' . $uuid;
136-
}
137127
}

src/WalletServiceProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Bavix\Wallet\Internal\Assembler\TransferQueryAssemblerInterface;
2828
use Bavix\Wallet\Internal\Assembler\WalletCreatedEventAssembler;
2929
use Bavix\Wallet\Internal\Assembler\WalletCreatedEventAssemblerInterface;
30+
use Bavix\Wallet\Internal\Decorator\StorageServiceLockDecorator;
3031
use Bavix\Wallet\Internal\Events\BalanceUpdatedEvent;
3132
use Bavix\Wallet\Internal\Events\BalanceUpdatedEventInterface;
3233
use Bavix\Wallet\Internal\Events\TransactionCreatedEvent;
@@ -247,7 +248,7 @@ private function services(array $configure, array $cache): void
247248
$configure['bookkeeper'] ?? BookkeeperService::class,
248249
[
249250
'storageService' => $this->app->make(
250-
StorageServiceInterface::class,
251+
StorageServiceLockDecorator::class,
251252
[
252253
'cacheRepository' => $this->app->make(CacheFactory::class)
253254
->store($cache['driver'] ?? 'array'),
@@ -262,7 +263,7 @@ private function services(array $configure, array $cache): void
262263
'storageService' => $this->app->make(
263264
StorageServiceInterface::class,
264265
[
265-
'cacheRepository' => $this->app->make(CacheFactory::class)
266+
'cacheRepository' => clone $this->app->make(CacheFactory::class)
266267
->store('array'),
267268
],
268269
),

0 commit comments

Comments
 (0)