Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.5] Optimize StateServiceInterface #582

Merged
merged 4 commits into from
Oct 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions src/Internal/Service/StateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ final class StateService implements StateServiceInterface

private const PREFIX_FORK_CALL = 'wallet_fork_call::';

private CacheRepository $forks;
private const PREFIX_HASHMAP = 'wallet_hm::';

private CacheRepository $forkCallables;
private CacheRepository $store;

public function __construct(CacheFactory $cacheFactory)
{
$this->forks = $cacheFactory->store('array');
$this->forkCallables = $cacheFactory->store('array');
$this->store = $cacheFactory->store('array');
}

/**
Expand All @@ -31,44 +30,61 @@ public function multiFork(array $uuids, callable $value): void
{
$forks = [];
foreach ($uuids as $uuid) {
if (! $this->forks->has(self::PREFIX_FORKS . $uuid)) {
if (! $this->store->has(self::PREFIX_FORKS . $uuid)) {
$forks[self::PREFIX_FORK_CALL . $uuid] = $value;
$forks[self::PREFIX_HASHMAP . $uuid] = $uuids;
}
}

if ($forks !== []) {
$this->forkCallables->setMultiple($forks);
$this->store->setMultiple($forks);
}
}

public function get(string $uuid): ?string
{
$value = $this->forks->get(self::PREFIX_FORKS . $uuid);
$value = $this->store->get(self::PREFIX_FORKS . $uuid);
if ($value !== null) {
return $value;
}

/** @var null|callable(): array<string, string> $callable */
$callable = $this->forkCallables->pull(self::PREFIX_FORK_CALL . $uuid);
$callable = $this->store->pull(self::PREFIX_FORK_CALL . $uuid);
if ($callable !== null) {
/** @var array<string> $keys */
$keys = $this->store->pull(self::PREFIX_HASHMAP . $uuid, []);
$deleteKeys = [];
foreach ($keys as $key) {
$deleteKeys[] = self::PREFIX_FORK_CALL . $key;
$deleteKeys[] = self::PREFIX_HASHMAP . $key;
}

if ($deleteKeys !== []) {
$this->store->deleteMultiple($deleteKeys);
}

$results = $callable();
$values = [];
foreach ($callable() as $key => $value) {
foreach ($results as $key => $value) {
$values[self::PREFIX_FORKS . $key] = $value;
}

if ($values !== []) {
$this->forks->setMultiple($values);

return $this->get($uuid);
$this->store->setMultiple($values);
}

return $results[$uuid] ?? null;
}

return null;
}

public function drop(string $uuid): void
{
$this->forkCallables->forget(self::PREFIX_FORK_CALL . $uuid);
$this->forks->forget(self::PREFIX_FORKS . $uuid);
$this->store->deleteMultiple([
self::PREFIX_FORK_CALL . $uuid,
self::PREFIX_HASHMAP . $uuid,
self::PREFIX_FORKS . $uuid,
]);
}
}