Skip to content

Commit 9c616df

Browse files
committed
optimize lock provider & storage service lock decorator
1 parent 64c55f3 commit 9c616df

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

src/Internal/Decorator/StorageServiceLockDecorator.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public function sync(string $uuid, float|int|string $value): bool
4343
*/
4444
public function increase(string $uuid, float|int|string $value): string
4545
{
46-
return $this->lockService->block(
47-
$uuid . '::increase',
48-
fn () => $this->storageService->increase($uuid, $value)
49-
);
46+
return $this->lockService->block($uuid, fn () => $this->storageService->increase($uuid, $value));
5047
}
5148
}

src/Internal/Service/LockService.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
final class LockService implements LockServiceInterface
1414
{
15+
private const PREFIX = 'wallet_lock::';
16+
1517
/**
1618
* @var array<string, bool>
1719
*/
@@ -32,19 +34,21 @@ public function __construct(CacheFactory $cacheFactory)
3234
*/
3335
public function block(string $key, callable $callback): mixed
3436
{
35-
if (array_key_exists($key, $this->lockedKeys)) {
37+
$lockKey = self::PREFIX . $key;
38+
39+
if (array_key_exists($lockKey, $this->lockedKeys)) {
3640
return $callback();
3741
}
3842

39-
$this->lockedKeys[$key] = true;
43+
$this->lockedKeys[$lockKey] = true;
4044

4145
try {
4246
return $this->getLockProvider()
43-
->lock($key)
47+
->lock($lockKey)
4448
->block($this->seconds, $callback)
4549
;
4650
} finally {
47-
unset($this->lockedKeys[$key]);
51+
unset($this->lockedKeys[$lockKey]);
4852
}
4953
}
5054

src/Services/AtomicService.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
*/
1818
final class AtomicService implements AtomicServiceInterface
1919
{
20-
private const PREFIX = 'wallet_atomic::';
21-
2220
public function __construct(
2321
private DatabaseServiceInterface $databaseService,
2422
private LockServiceInterface $lockService,
@@ -38,7 +36,8 @@ public function blocks(array $objects, callable $callback): mixed
3836
{
3937
$callable = fn () => $this->databaseService->transaction($callback);
4038
foreach ($objects as $object) {
41-
$callable = fn () => $this->lockService->block($this->key($object), $callable);
39+
$wallet = $this->castService->getWallet($object);
40+
$callable = fn () => $this->lockService->block($wallet->uuid, $callable);
4241
}
4342

4443
return $callable();
@@ -54,11 +53,4 @@ public function block(Wallet $object, callable $callback): mixed
5453
{
5554
return $this->blocks([$object], $callback);
5655
}
57-
58-
private function key(Wallet $object): string
59-
{
60-
$wallet = $this->castService->getWallet($object);
61-
62-
return self::PREFIX . '::' . $wallet::class . '::' . $wallet->uuid;
63-
}
6456
}

0 commit comments

Comments
 (0)