Skip to content

Commit a0059c9

Browse files
authored
Store related users in the Redis (#28)
1 parent f0674e1 commit a0059c9

File tree

8 files changed

+141
-5
lines changed

8 files changed

+141
-5
lines changed

src/DB/Dictionary/ParallelRunningDictionary.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ParallelRunningDictionary
88
{
99
public const IS_INITIAZLIED_KEY = 'parallelRunningIsInitialized';
1010
public const IS_RUNNING_KEY = 'parallelRunningReadyToServe';
11+
public const RELATED_USERS_KEY = 'parallelRunningRelatedUsers';
1112

1213
public const IS_INITIAZLIED_TRUE_VALUE = 'initialized';
1314
public const IS_RUNNING_VALUE_TRUE = 'ready';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Abrouter\Client\DB\Managers;
6+
7+
use Abrouter\Client\Config\Accessors\KvStorageConfigAccessor;
8+
use Abrouter\Client\DB\Dictionary\ParallelRunningDictionary;
9+
use Abrouter\Client\Services\KvStorage\KvStorage;
10+
11+
class RelatedUsersCacheManager
12+
{
13+
private KvStorageConfigAccessor $kvStorage;
14+
15+
public function __construct(
16+
KvStorageConfigAccessor $kvStorage
17+
) {
18+
$this->kvStorage = $kvStorage;
19+
}
20+
21+
public function store(array $relatedUsers): void
22+
{
23+
$this
24+
->kvStorage
25+
->getKvStorage()
26+
->put(ParallelRunningDictionary::RELATED_USERS_KEY, json_encode($relatedUsers));
27+
}
28+
}

src/DB/RelatedUsersStore.php

+5
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public static function load(array $relatedUsersList): void
2727
{
2828
self::$store = new RelatedUsersCollection($relatedUsersList);
2929
}
30+
31+
public static function isLoaded(): bool
32+
{
33+
return self::$store !== null;
34+
}
3035
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Abrouter\Client\DB\Repositories;
6+
7+
use Abrouter\Client\Config\Accessors\KvStorageConfigAccessor;
8+
use Abrouter\Client\DB\Dictionary\ParallelRunningDictionary;
9+
use Abrouter\Client\Services\KvStorage\KvStorage;
10+
11+
class RelatedUsersCacheRepository
12+
{
13+
private KvStorageConfigAccessor $kvStorage;
14+
15+
public function __construct(
16+
KvStorageConfigAccessor $kvStorage
17+
) {
18+
$this->kvStorage = $kvStorage;
19+
}
20+
21+
public function getAll(): array
22+
{
23+
$json = $this->kvStorage->getKvStorage()->get(ParallelRunningDictionary::RELATED_USERS_KEY);
24+
return $json === null ? [] : json_decode($json, true);
25+
}
26+
}

src/Events/Handlers/RelatedUsersStatisticsInterceptor.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Abrouter\Client\Events\Handlers;
66

77
use Abrouter\Client\Contracts\TaskContract;
8+
use Abrouter\Client\DB\Managers\RelatedUsersCacheManager;
89
use Abrouter\Client\DB\RelatedUsersStore;
910
use Abrouter\Client\Events\HandlerInterface;
1011
use Abrouter\Client\Services\ExperimentsParallelRun\ParallelRunSwitch;
@@ -16,12 +17,16 @@ class RelatedUsersStatisticsInterceptor implements HandlerInterface
1617

1718
private RelatedUsersStore $relatedUsersStore;
1819

20+
private RelatedUsersCacheManager $relatedUsersCacheManager;
21+
1922
public function __construct(
2023
ParallelRunSwitch $parallelRunSwitch,
21-
RelatedUsersStore $relatedUsersStore
24+
RelatedUsersStore $relatedUsersStore,
25+
RelatedUsersCacheManager $relatedUsersCacheManager
2226
) {
2327
$this->parallelRunSwitch = $parallelRunSwitch;
2428
$this->relatedUsersStore = $relatedUsersStore;
29+
$this->relatedUsersCacheManager = $relatedUsersCacheManager;
2530
}
2631

2732
public function handle(TaskContract $taskContract): bool
@@ -38,6 +43,10 @@ public function handle(TaskContract $taskContract): bool
3843
$temporaryUserId = $taskContract->getEventDTO()->getBaseEventDTO()->getTemporaryUserId();
3944

4045
$this->relatedUsersStore->get()->append($userId, $temporaryUserId);
46+
$this->relatedUsersCacheManager->store(
47+
$this->relatedUsersStore->get()->getAll()
48+
);
49+
4150

4251
return true;
4352
}

src/Services/ExperimentsParallelRun/ParallelRunInitializer.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,33 @@
77
use Abrouter\Client\DB\Managers\ParallelRunningStateManager;
88
use Abrouter\Client\DB\RelatedUsersStore;
99
use Abrouter\Client\DB\Repositories\ParallelRunningStateCachedRepository;
10+
use Abrouter\Client\DB\Repositories\RelatedUsersCacheRepository;
1011

1112
class ParallelRunInitializer
1213
{
1314
private ParallelRunningStateCachedRepository $parallelRunningStateCachedRepository;
1415

1516
private ParallelRunningStateManager $parallelRunningStateManager;
1617

18+
private RelatedUsersCacheRepository $relatedUsersCacheRepository;
19+
1720
public function __construct(
1821
ParallelRunningStateCachedRepository $parallelRunningStateCachedRepository,
19-
ParallelRunningStateManager $parallelRunningStateManager
22+
ParallelRunningStateManager $parallelRunningStateManager,
23+
RelatedUsersCacheRepository $relatedUsersCacheRepository
2024
) {
2125
$this->parallelRunningStateCachedRepository = $parallelRunningStateCachedRepository;
2226
$this->parallelRunningStateManager = $parallelRunningStateManager;
27+
$this->relatedUsersCacheRepository = $relatedUsersCacheRepository;
2328
}
2429

2530
public function initializeIfNot(): bool
2631
{
32+
if (!RelatedUsersStore::isLoaded()) {
33+
//initialization related users
34+
RelatedUsersStore::load($this->relatedUsersCacheRepository->getAll());
35+
}
36+
2737
//if parallel running ready to serve
2838
if ($this->parallelRunningStateCachedRepository->isReady()) {
2939
return true;
@@ -34,9 +44,6 @@ public function initializeIfNot(): bool
3444
return false;
3545
}
3646

37-
//initialization
38-
RelatedUsersStore::load([]);
39-
4047
$this->parallelRunningStateManager->setInitialized();
4148
$this->parallelRunningStateManager->setReadyToServe();
4249

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Abrouter\Client\Tests\Integration\DB\Managers;
6+
7+
use Abrouter\Client\DB\Dictionary\ParallelRunningDictionary;
8+
use Abrouter\Client\DB\RedisConnection;
9+
use Abrouter\Client\DB\Repositories\RelatedUsersCacheRepository;
10+
use Abrouter\Client\Tests\Integration\IntegrationTestCase;
11+
12+
class RelatedUsersCacheManagerTest extends IntegrationTestCase
13+
{
14+
public function testSavingSomeUsers()
15+
{
16+
$this->configureParallelRun('default');
17+
$this->clearRedis();
18+
19+
$relatedUsersList = ['test' => ['test2']];
20+
$relatedUsersCacheRepository = $this->getContainer()->make(RelatedUsersCacheRepository::class);
21+
$redis = $this->getContainer()->make(RedisConnection::class);
22+
$redis->getConnection()->set(
23+
ParallelRunningDictionary::RELATED_USERS_KEY,
24+
json_encode($relatedUsersList)
25+
);
26+
27+
$this->assertEquals($relatedUsersCacheRepository->getAll(), $relatedUsersList);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Abrouter\Client\Tests\Integration\DB\Repositories;
6+
7+
use Abrouter\Client\DB\Dictionary\ParallelRunningDictionary;
8+
use Abrouter\Client\DB\Managers\RelatedUsersCacheManager;
9+
use Abrouter\Client\DB\RedisConnection;
10+
use Abrouter\Client\Tests\Integration\IntegrationTestCase;
11+
12+
class RelatedUsersCacheRepositoryTest extends IntegrationTestCase
13+
{
14+
public function testGet()
15+
{
16+
$this->configureParallelRun('default');
17+
$this->clearRedis();
18+
19+
$usersList = [
20+
'test' => ['test'],
21+
];
22+
$relatedUsersCacheManager = $this->getContainer()->make(RelatedUsersCacheManager::class);
23+
$relatedUsersCacheManager->store($usersList);
24+
25+
$redis = $this->getContainer()->make(RedisConnection::class);
26+
$this->assertEquals(
27+
$usersList,
28+
json_decode($redis->getConnection()->get(ParallelRunningDictionary::RELATED_USERS_KEY), true)
29+
);
30+
}
31+
}

0 commit comments

Comments
 (0)