From 126528888bfa1e44f66803d2a08b9f08399243db Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Fri, 12 Jan 2024 18:24:20 -0300 Subject: [PATCH 1/5] added cache for bySet evaluation --- src/Client.php | 14 ++++++++++++-- src/Utils/EvalCache/Cache.php | 2 ++ src/Utils/EvalCache/CacheImpl.php | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index bbab11d..dded10f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -166,7 +166,12 @@ public function getTreatmentsByFlagSet( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_BY_FLAG_SET; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - // @TODO implement cache for this method + $toReturn = $this->cache->getByFlagSets([$flagSet], $key, $attributes, false); + $features = self::getMissing($toReturn); + + if (count($features) == 0) { + return $toReturn; + } $this->tracer->trace(TEF::forRPCStart($method, $id)); $results = $this->lm->getTreatmentsByFlagSet($key, $bucketingKey, $flagSet, $attributes); @@ -197,7 +202,12 @@ public function getTreatmentsWithConfigByFlagSet( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - // @TODO implement cache for this method + $toReturn = $this->cache->getByFlagSets([$flagSet], $key, $attributes, true); + $features = self::getMissing($toReturn); + + if (count($features) == 0) { + return $toReturn; + } $this->tracer->trace(TEF::forRPCStart($method, $id)); $results = $this->lm->getTreatmentsWithConfigByFlagSet($key, $bucketingKey, $flagSet, $attributes); diff --git a/src/Utils/EvalCache/Cache.php b/src/Utils/EvalCache/Cache.php index 5087981..c4274ab 100644 --- a/src/Utils/EvalCache/Cache.php +++ b/src/Utils/EvalCache/Cache.php @@ -8,8 +8,10 @@ public function get(string $key, string $feature, ?array $attributes): ?string; public function getMany(string $key, array $features, ?array $attributes): array; public function getWithConfig(string $key, string $feature, ?array $attributes): ?array; public function getManyWithConfig(string $key, array $features, ?array $attributes): array; + public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): array; public function set(string $key, string $feature, ?array $attributes, string $treatment); public function setMany(string $key, ?array $attributes, array $results); public function setWithConfig(string $key, string $feature, ?array $attributes, string $treatment, ?string $config); public function setManyWithConfig(string $key, ?array $attributes, array $results); + public function setFeaturesForFlagSets(string $key, array $flagSets, ?array $attributes, array $results, bool $withConfig); } diff --git a/src/Utils/EvalCache/CacheImpl.php b/src/Utils/EvalCache/CacheImpl.php index a40c0b6..4125e7a 100644 --- a/src/Utils/EvalCache/CacheImpl.php +++ b/src/Utils/EvalCache/CacheImpl.php @@ -8,6 +8,7 @@ class CacheImpl implements Cache private /*array*/ $data; private /*InputHasher*/ $hasher; private /*EvictionPolicy*/ $evictionPolicy; + private /*array*/ $flagSets; public function __construct(InputHasher $hasher, EvictionPolicy $evictionPolicy) { @@ -50,6 +51,17 @@ public function getManyWithConfig(string $key, array $features, ?array $attribut return $result; } + public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): array + { + sort($flagSets); + $h = implode(",", $flagSets); + $features = $this->flagSets[$h] ?? null; + if (is_null($features)) { + return []; + } + return $withConfig ? $this->getManyWithConfig($key, $$features, $attributes) : $this->getMany($key, $$features, $attributes); + } + public function set(string $key, string $feature, ?array $attributes, string $treatment) { $h = $this->hasher->hashInput($key, $feature, $attributes); @@ -78,6 +90,17 @@ public function setManyWithConfig(string $key, ?array $attributes, array $result } } + public function setFeaturesForFlagSets(string $key, array $flagSets, ?array $attributes, array $results, bool $withConfig) + { + $h = implode(",", $flagSets); + $this->flagSets[$h] = array_keys($results); + if ($withConfig) { + $this->setManyWithConfig($key, $attributes, $results); + return; + } + $this->setMany($key, $attributes, $results); + } + private function _get(string $key, string $feature, ?array $attributes): ?Entry { $h = $this->hasher->hashInput($key, $feature, $attributes); From b10f8d06224d7c6054ccb69a981e9c8da6aa6a02 Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Sun, 14 Jan 2024 19:05:01 -0300 Subject: [PATCH 2/5] added cache for flagsets --- src/Client.php | 18 ++++++----- src/Utils/EvalCache/Cache.php | 2 +- src/Utils/EvalCache/CacheImpl.php | 6 ++-- src/Utils/EvalCache/NoCache.php | 9 ++++++ tests/Utils/EvalCache/CacheImplTest.php | 40 +++++++++++++++++++++++++ tests/Utils/EvalCache/NoCacheTest.php | 18 +++++++++++ 6 files changed, 81 insertions(+), 12 deletions(-) diff --git a/src/Client.php b/src/Client.php index db7938c..2288402 100644 --- a/src/Client.php +++ b/src/Client.php @@ -167,9 +167,7 @@ public function getTreatmentsByFlagSet( $method = Tracer::METHOD_GET_TREATMENTS_BY_FLAG_SET; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); $toReturn = $this->cache->getByFlagSets([$flagSet], $key, $attributes, false); - $features = self::getMissing($toReturn); - - if (count($features) == 0) { + if (!is_null($toReturn)) { return $toReturn; } @@ -203,9 +201,7 @@ public function getTreatmentsWithConfigByFlagSet( $method = Tracer::METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); $toReturn = $this->cache->getByFlagSets([$flagSet], $key, $attributes, true); - $features = self::getMissing($toReturn); - - if (count($features) == 0) { + if (!is_null($toReturn)) { return $toReturn; } @@ -238,7 +234,10 @@ public function getTreatmentsByFlagSets( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_BY_FLAG_SETS; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - // @TODO implement cache for this method + $toReturn = $this->cache->getByFlagSets($flagSets, $key, $attributes, false); + if (!is_null($toReturn)) { + return $toReturn; + } $this->tracer->trace(TEF::forRPCStart($method, $id)); $results = $this->lm->getTreatmentsByFlagSets($key, $bucketingKey, $flagSets, $attributes); @@ -269,7 +268,10 @@ public function getTreatmentsWithConfigByFlagSets( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - // @TODO implement cache for this method + $toReturn = $this->cache->getByFlagSets($flagSets, $key, $attributes, true); + if (!is_null($toReturn)) { + return $toReturn; + } $this->tracer->trace(TEF::forRPCStart($method, $id)); $results = $this->lm->getTreatmentsWithConfigByFlagSets($key, $bucketingKey, $flagSets, $attributes); diff --git a/src/Utils/EvalCache/Cache.php b/src/Utils/EvalCache/Cache.php index c4274ab..5bbe4af 100644 --- a/src/Utils/EvalCache/Cache.php +++ b/src/Utils/EvalCache/Cache.php @@ -8,7 +8,7 @@ public function get(string $key, string $feature, ?array $attributes): ?string; public function getMany(string $key, array $features, ?array $attributes): array; public function getWithConfig(string $key, string $feature, ?array $attributes): ?array; public function getManyWithConfig(string $key, array $features, ?array $attributes): array; - public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): array; + public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): ?array; public function set(string $key, string $feature, ?array $attributes, string $treatment); public function setMany(string $key, ?array $attributes, array $results); public function setWithConfig(string $key, string $feature, ?array $attributes, string $treatment, ?string $config); diff --git a/src/Utils/EvalCache/CacheImpl.php b/src/Utils/EvalCache/CacheImpl.php index 4125e7a..0e61b1b 100644 --- a/src/Utils/EvalCache/CacheImpl.php +++ b/src/Utils/EvalCache/CacheImpl.php @@ -51,15 +51,15 @@ public function getManyWithConfig(string $key, array $features, ?array $attribut return $result; } - public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): array + public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): ?array { sort($flagSets); $h = implode(",", $flagSets); $features = $this->flagSets[$h] ?? null; if (is_null($features)) { - return []; + return null; } - return $withConfig ? $this->getManyWithConfig($key, $$features, $attributes) : $this->getMany($key, $$features, $attributes); + return $withConfig ? $this->getManyWithConfig($key, $features, $attributes) : $this->getMany($key, $features, $attributes); } public function set(string $key, string $feature, ?array $attributes, string $treatment) diff --git a/src/Utils/EvalCache/NoCache.php b/src/Utils/EvalCache/NoCache.php index 5132086..777aaa6 100644 --- a/src/Utils/EvalCache/NoCache.php +++ b/src/Utils/EvalCache/NoCache.php @@ -32,6 +32,11 @@ public function getManyWithConfig(string $key, array $features, ?array $attribut return $res; } + public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): ?array + { + return null; + } + public function set(string $key, string $feature, ?array $attributes, string $treatment) { } @@ -47,4 +52,8 @@ public function setWithConfig(string $key, string $feature, ?array $attributes, public function setManyWithConfig(string $key, ?array $attributes, array $results) { } + + public function setFeaturesForFlagSets(string $key, array $flagSets, ?array $attributes, array $results, bool $withConfig) + { + } } diff --git a/tests/Utils/EvalCache/CacheImplTest.php b/tests/Utils/EvalCache/CacheImplTest.php index 8bfdf9f..d0b0067 100644 --- a/tests/Utils/EvalCache/CacheImplTest.php +++ b/tests/Utils/EvalCache/CacheImplTest.php @@ -76,4 +76,44 @@ public function testWithConfig() // nothing matches for [] attributes $this->assertEquals(['f1' => null, 'f2' => null, 'f3' => null], $c->getManyWithConfig('key', ['f1', 'f2', 'f3'], [])); } + + public function testByFlagSetsWithoutConfig() + { + $c = new CacheImpl(new KeyAttributeCRC32Hasher(), new NoEviction(0)); + $c->setFeaturesForFlagSets('key', ['s1', 's2'], null, ['f1' => 'on', 'f2' => 'off'], false); + $this->assertEquals('on', $c->get('key', 'f1', null)); + $this->assertEquals(null, $c->get('key2', 'f1', null)); + $this->assertEquals(null, $c->get('key', 'f1', ['a' => 1])); + $this->assertEquals('off', $c->get('key', 'f2', null)); + + $results = $c->getByFlagSets(['s1', 's2'], 'key', null, false); + $this->assertEquals(2, count($results)); + $this->assertEquals('on', $results['f1']); + $this->assertEquals('off', $results['f2']); + + $results = $c->getByFlagSets(['s1'], 'key', null, false); + $this->assertEquals(null, $results); + $results = $c->getByFlagSets(['s2'], 'key', null, false); + $this->assertEquals(null, $results); + } + + public function testByFlagSetsWithConfig() + { + $c = new CacheImpl(new KeyAttributeCRC32Hasher(), new NoEviction(0)); + $c->setFeaturesForFlagSets('key', ['s1', 's2'], null, ['f1' => ['treatment' => 'on', 'config' => 'some'], 'f2' => ['treatment' => 'off', 'config' => null]], true); + $this->assertEquals(['treatment' => 'on', 'config' => 'some'], $c->getWithConfig('key', 'f1', null)); + $this->assertEquals(null, $c->getWithConfig('key2', 'f1', null)); + $this->assertEquals(null, $c->getWithConfig('key', 'f1', ['a' => 1])); + $this->assertEquals(['treatment' => 'off', 'config' => null], $c->getWithConfig('key', 'f2', null)); + + $results = $c->getByFlagSets(['s1', 's2'], 'key', null, true); + $this->assertEquals(2, count($results)); + $this->assertEquals(['treatment' => 'on', 'config' => 'some'], $results['f1']); + $this->assertEquals(['treatment' => 'off', 'config' => null], $results['f2']); + + $results = $c->getByFlagSets(['s1'], 'key', null, false); + $this->assertEquals(null, $results); + $results = $c->getByFlagSets(['s2'], 'key', null, false); + $this->assertEquals(null, $results); + } } diff --git a/tests/Utils/EvalCache/NoCacheTest.php b/tests/Utils/EvalCache/NoCacheTest.php index a751339..220bd4f 100644 --- a/tests/Utils/EvalCache/NoCacheTest.php +++ b/tests/Utils/EvalCache/NoCacheTest.php @@ -39,5 +39,23 @@ public function testNoCache() $this->assertEquals(null, $c->getWithConfig('key', 'f1', null)); $this->assertEquals(null, $c->getWithConfig('key', 'f2', null)); $this->assertEquals(['f1' => null, 'f2' => null], $c->getManyWithConfig('key', ['f1', 'f2'], null)); + + $c->setFeaturesForFlagSets('key', ['f1', 'f2'], null, [ + 'f1' => 'on', + 'f2' => 'off', + ], false); + $this->assertEquals(null, $c->get('key', 'f1', null)); + $this->assertEquals(null, $c->get('key', 'f2', ['a' => 1])); + $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, false)); + $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, true)); + + $c->setFeaturesForFlagSets('key', ['f1', 'f2'], null, [ + 'f1' => ['treatment' => 'on', 'config' => 'some'], + 'f2' => ['treatment' => 'off', 'config' => null], + ], true); + $this->assertEquals(null, $c->get('key', 'f1', null)); + $this->assertEquals(null, $c->get('key', 'f2', ['a' => 1])); + $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, false)); + $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, true)); } } From 29f85801012c324cf9bb01ff6a430c00672869e6 Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Sun, 14 Jan 2024 19:07:47 -0300 Subject: [PATCH 3/5] added some comments --- src/Utils/EvalCache/CacheImpl.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Utils/EvalCache/CacheImpl.php b/src/Utils/EvalCache/CacheImpl.php index 0e61b1b..df1eca2 100644 --- a/src/Utils/EvalCache/CacheImpl.php +++ b/src/Utils/EvalCache/CacheImpl.php @@ -53,10 +53,10 @@ public function getManyWithConfig(string $key, array $features, ?array $attribut public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): ?array { - sort($flagSets); - $h = implode(",", $flagSets); + sort($flagSets); // Order flagSets to store + $h = implode(",", $flagSets); // Concatenating each flagSet with a comma e.g: flagSet1,flagSet2,flagSet3 $features = $this->flagSets[$h] ?? null; - if (is_null($features)) { + if (is_null($features)) { // if null then no flagSets were stored return null; } return $withConfig ? $this->getManyWithConfig($key, $features, $attributes) : $this->getMany($key, $features, $attributes); @@ -92,8 +92,9 @@ public function setManyWithConfig(string $key, ?array $attributes, array $result public function setFeaturesForFlagSets(string $key, array $flagSets, ?array $attributes, array $results, bool $withConfig) { - $h = implode(",", $flagSets); - $this->flagSets[$h] = array_keys($results); + $h = implode(",", $flagSets); // Concatenating each flagSet with a comma e.g: flagSet1,flagSet2,flagSet3 + $this->flagSets[$h] = array_keys($results); // Get all the features to store the list of features associated to the flagSets + // Store the evaluation results for each feature if ($withConfig) { $this->setManyWithConfig($key, $attributes, $results); return; From 08479a0f25dbf197c8b04e86791041289df760df Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Mon, 15 Jan 2024 13:57:38 -0300 Subject: [PATCH 4/5] updated cache methods for flagsets --- src/Client.php | 49 +++++++++++++++++-------- src/Utils/EvalCache/Cache.php | 4 +- src/Utils/EvalCache/CacheImpl.php | 22 +++-------- src/Utils/EvalCache/NoCache.php | 4 +- tests/Utils/EvalCache/CacheImplTest.php | 41 +++------------------ tests/Utils/EvalCache/NoCacheTest.php | 21 ++--------- 6 files changed, 53 insertions(+), 88 deletions(-) diff --git a/src/Client.php b/src/Client.php index 2288402..55fe318 100644 --- a/src/Client.php +++ b/src/Client.php @@ -129,7 +129,6 @@ public function getTreatmentsWithConfig(string $key, ?string $bucketingKey, arra $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); $toReturn = $this->cache->getManyWithConfig($key, $features, $attributes); $features = self::getMissing($toReturn); - if (count($features) == 0) { return $toReturn; } @@ -166,9 +165,13 @@ public function getTreatmentsByFlagSet( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_BY_FLAG_SET; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - $toReturn = $this->cache->getByFlagSets([$flagSet], $key, $attributes, false); - if (!is_null($toReturn)) { - return $toReturn; + $featuresFromSet = $this->cache->getFeaturesByFlagSets([$flagSet]); + if (!is_null($featuresFromSet)) { + $toReturn = $this->cache->getMany($key, $featuresFromSet, $attributes); + $features = self::getMissing($toReturn); + if (count($features) == 0) { + return $toReturn; + } } $this->tracer->trace(TEF::forRPCStart($method, $id)); @@ -179,6 +182,7 @@ public function getTreatmentsByFlagSet( $toReturn[$feature] = $treatment; $this->handleListener($key, $bucketingKey, $feature, $attributes, $treatment, $ilData); } + $this->cache->setFeaturesForFlagSets([$flagSet], array_keys($results)); $this->cache->setMany($key, $attributes, $toReturn); return $toReturn; } catch (\Exception $exc) { @@ -200,9 +204,13 @@ public function getTreatmentsWithConfigByFlagSet( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - $toReturn = $this->cache->getByFlagSets([$flagSet], $key, $attributes, true); - if (!is_null($toReturn)) { - return $toReturn; + $featuresFromSet = $this->cache->getFeaturesByFlagSets([$flagSet]); + if (!is_null($featuresFromSet)) { + $toReturn = $this->cache->getManyWithConfig($key, $featuresFromSet, $attributes); + $features = self::getMissing($toReturn); + if (count($features) == 0) { + return $toReturn; + } } $this->tracer->trace(TEF::forRPCStart($method, $id)); @@ -213,7 +221,8 @@ public function getTreatmentsWithConfigByFlagSet( $toReturn[$feature] = ['treatment' => $treatment, 'config' => $config]; $this->handleListener($key, $bucketingKey, $feature, $attributes, $treatment, $ilData); } - $this->cache->setMany($key, $attributes, $toReturn); + $this->cache->setFeaturesForFlagSets([$flagSet], array_keys($results)); + $this->cache->setManyWithConfig($key, $attributes, $toReturn); return $toReturn; } catch (\Exception $exc) { $this->tracer->trace(TEF::forException($method, $id, $exc)); @@ -234,9 +243,13 @@ public function getTreatmentsByFlagSets( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_BY_FLAG_SETS; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - $toReturn = $this->cache->getByFlagSets($flagSets, $key, $attributes, false); - if (!is_null($toReturn)) { - return $toReturn; + $featuresFromSets = $this->cache->getFeaturesByFlagSets($flagSets); + if (!is_null($featuresFromSets)) { + $toReturn = $this->cache->getMany($key, $featuresFromSets, $attributes); + $features = self::getMissing($toReturn); + if (count($features) == 0) { + return $toReturn; + } } $this->tracer->trace(TEF::forRPCStart($method, $id)); @@ -247,6 +260,7 @@ public function getTreatmentsByFlagSets( $toReturn[$feature] = $treatment; $this->handleListener($key, $bucketingKey, $feature, $attributes, $treatment, $ilData); } + $this->cache->setFeaturesForFlagSets($flagSets, array_keys($results)); $this->cache->setMany($key, $attributes, $toReturn); return $toReturn; } catch (\Exception $exc) { @@ -268,9 +282,13 @@ public function getTreatmentsWithConfigByFlagSets( $id = $this->tracer->makeId(); $method = Tracer::METHOD_GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS; $this->tracer->trace(TEF::forStart($method, $id, $this->tracer->includeArgs() ? func_get_args() : [])); - $toReturn = $this->cache->getByFlagSets($flagSets, $key, $attributes, true); - if (!is_null($toReturn)) { - return $toReturn; + $featuresFromSet = $this->cache->getFeaturesByFlagSets($flagSets); + if (!is_null($featuresFromSet)) { + $toReturn = $this->cache->getManyWithConfig($key, $featuresFromSet, $attributes); + $features = self::getMissing($toReturn); + if (count($features) == 0) { + return $toReturn; + } } $this->tracer->trace(TEF::forRPCStart($method, $id)); @@ -281,7 +299,8 @@ public function getTreatmentsWithConfigByFlagSets( $toReturn[$feature] = ['treatment' => $treatment, 'config' => $config]; $this->handleListener($key, $bucketingKey, $feature, $attributes, $treatment, $ilData); } - $this->cache->setMany($key, $attributes, $toReturn); + $this->cache->setFeaturesForFlagSets($flagSets, array_keys($results)); + $this->cache->setManyWithConfig($key, $attributes, $toReturn); return $toReturn; } catch (\Exception $exc) { $this->tracer->trace(TEF::forException($method, $id, $exc)); diff --git a/src/Utils/EvalCache/Cache.php b/src/Utils/EvalCache/Cache.php index 5bbe4af..c917b2c 100644 --- a/src/Utils/EvalCache/Cache.php +++ b/src/Utils/EvalCache/Cache.php @@ -8,10 +8,10 @@ public function get(string $key, string $feature, ?array $attributes): ?string; public function getMany(string $key, array $features, ?array $attributes): array; public function getWithConfig(string $key, string $feature, ?array $attributes): ?array; public function getManyWithConfig(string $key, array $features, ?array $attributes): array; - public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): ?array; + public function getFeaturesByFlagSets(array $flagSets): ?array; public function set(string $key, string $feature, ?array $attributes, string $treatment); public function setMany(string $key, ?array $attributes, array $results); public function setWithConfig(string $key, string $feature, ?array $attributes, string $treatment, ?string $config); public function setManyWithConfig(string $key, ?array $attributes, array $results); - public function setFeaturesForFlagSets(string $key, array $flagSets, ?array $attributes, array $results, bool $withConfig); + public function setFeaturesForFlagSets(array $flagSets, array $featuresFlags); } diff --git a/src/Utils/EvalCache/CacheImpl.php b/src/Utils/EvalCache/CacheImpl.php index df1eca2..303b216 100644 --- a/src/Utils/EvalCache/CacheImpl.php +++ b/src/Utils/EvalCache/CacheImpl.php @@ -51,17 +51,12 @@ public function getManyWithConfig(string $key, array $features, ?array $attribut return $result; } - public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): ?array + public function getFeaturesByFlagSets(array $flagSets): ?array { - sort($flagSets); // Order flagSets to store + sort($flagSets); // Order flagSets to grab from store $h = implode(",", $flagSets); // Concatenating each flagSet with a comma e.g: flagSet1,flagSet2,flagSet3 - $features = $this->flagSets[$h] ?? null; - if (is_null($features)) { // if null then no flagSets were stored - return null; - } - return $withConfig ? $this->getManyWithConfig($key, $features, $attributes) : $this->getMany($key, $features, $attributes); + return $this->flagSets[$h] ?? null; } - public function set(string $key, string $feature, ?array $attributes, string $treatment) { $h = $this->hasher->hashInput($key, $feature, $attributes); @@ -90,16 +85,11 @@ public function setManyWithConfig(string $key, ?array $attributes, array $result } } - public function setFeaturesForFlagSets(string $key, array $flagSets, ?array $attributes, array $results, bool $withConfig) + public function setFeaturesForFlagSets(array $flagSets, array $featuresFlags) { + sort($flagSets); // Order flagSets to store $h = implode(",", $flagSets); // Concatenating each flagSet with a comma e.g: flagSet1,flagSet2,flagSet3 - $this->flagSets[$h] = array_keys($results); // Get all the features to store the list of features associated to the flagSets - // Store the evaluation results for each feature - if ($withConfig) { - $this->setManyWithConfig($key, $attributes, $results); - return; - } - $this->setMany($key, $attributes, $results); + $this->flagSets[$h] = $featuresFlags; } private function _get(string $key, string $feature, ?array $attributes): ?Entry diff --git a/src/Utils/EvalCache/NoCache.php b/src/Utils/EvalCache/NoCache.php index 777aaa6..bf3637d 100644 --- a/src/Utils/EvalCache/NoCache.php +++ b/src/Utils/EvalCache/NoCache.php @@ -32,7 +32,7 @@ public function getManyWithConfig(string $key, array $features, ?array $attribut return $res; } - public function getByFlagSets(array $flagSets, string $key, ?array $attributes, bool $withConfig): ?array + public function getFeaturesByFlagSets(array $flagSets): ?array { return null; } @@ -53,7 +53,7 @@ public function setManyWithConfig(string $key, ?array $attributes, array $result { } - public function setFeaturesForFlagSets(string $key, array $flagSets, ?array $attributes, array $results, bool $withConfig) + public function setFeaturesForFlagSets(array $flagSets, array $featuresFlags) { } } diff --git a/tests/Utils/EvalCache/CacheImplTest.php b/tests/Utils/EvalCache/CacheImplTest.php index d0b0067..47e0e3d 100644 --- a/tests/Utils/EvalCache/CacheImplTest.php +++ b/tests/Utils/EvalCache/CacheImplTest.php @@ -77,43 +77,12 @@ public function testWithConfig() $this->assertEquals(['f1' => null, 'f2' => null, 'f3' => null], $c->getManyWithConfig('key', ['f1', 'f2', 'f3'], [])); } - public function testByFlagSetsWithoutConfig() + public function testByFlagSets() { $c = new CacheImpl(new KeyAttributeCRC32Hasher(), new NoEviction(0)); - $c->setFeaturesForFlagSets('key', ['s1', 's2'], null, ['f1' => 'on', 'f2' => 'off'], false); - $this->assertEquals('on', $c->get('key', 'f1', null)); - $this->assertEquals(null, $c->get('key2', 'f1', null)); - $this->assertEquals(null, $c->get('key', 'f1', ['a' => 1])); - $this->assertEquals('off', $c->get('key', 'f2', null)); - - $results = $c->getByFlagSets(['s1', 's2'], 'key', null, false); - $this->assertEquals(2, count($results)); - $this->assertEquals('on', $results['f1']); - $this->assertEquals('off', $results['f2']); - - $results = $c->getByFlagSets(['s1'], 'key', null, false); - $this->assertEquals(null, $results); - $results = $c->getByFlagSets(['s2'], 'key', null, false); - $this->assertEquals(null, $results); - } - - public function testByFlagSetsWithConfig() - { - $c = new CacheImpl(new KeyAttributeCRC32Hasher(), new NoEviction(0)); - $c->setFeaturesForFlagSets('key', ['s1', 's2'], null, ['f1' => ['treatment' => 'on', 'config' => 'some'], 'f2' => ['treatment' => 'off', 'config' => null]], true); - $this->assertEquals(['treatment' => 'on', 'config' => 'some'], $c->getWithConfig('key', 'f1', null)); - $this->assertEquals(null, $c->getWithConfig('key2', 'f1', null)); - $this->assertEquals(null, $c->getWithConfig('key', 'f1', ['a' => 1])); - $this->assertEquals(['treatment' => 'off', 'config' => null], $c->getWithConfig('key', 'f2', null)); - - $results = $c->getByFlagSets(['s1', 's2'], 'key', null, true); - $this->assertEquals(2, count($results)); - $this->assertEquals(['treatment' => 'on', 'config' => 'some'], $results['f1']); - $this->assertEquals(['treatment' => 'off', 'config' => null], $results['f2']); - - $results = $c->getByFlagSets(['s1'], 'key', null, false); - $this->assertEquals(null, $results); - $results = $c->getByFlagSets(['s2'], 'key', null, false); - $this->assertEquals(null, $results); + $c->setFeaturesForFlagSets(['s2', 's1'], ['f1', 'f2']); + $this->assertEquals(['f1', 'f2'], $c->getFeaturesByFlagSets(['s1', 's2'])); + $this->assertEquals(['f1', 'f2'], $c->getFeaturesByFlagSets(['s2', 's1'])); + $this->assertEquals(null, $c->getFeaturesByFlagSets(['s2'])); } } diff --git a/tests/Utils/EvalCache/NoCacheTest.php b/tests/Utils/EvalCache/NoCacheTest.php index 220bd4f..88593ca 100644 --- a/tests/Utils/EvalCache/NoCacheTest.php +++ b/tests/Utils/EvalCache/NoCacheTest.php @@ -40,22 +40,9 @@ public function testNoCache() $this->assertEquals(null, $c->getWithConfig('key', 'f2', null)); $this->assertEquals(['f1' => null, 'f2' => null], $c->getManyWithConfig('key', ['f1', 'f2'], null)); - $c->setFeaturesForFlagSets('key', ['f1', 'f2'], null, [ - 'f1' => 'on', - 'f2' => 'off', - ], false); - $this->assertEquals(null, $c->get('key', 'f1', null)); - $this->assertEquals(null, $c->get('key', 'f2', ['a' => 1])); - $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, false)); - $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, true)); - - $c->setFeaturesForFlagSets('key', ['f1', 'f2'], null, [ - 'f1' => ['treatment' => 'on', 'config' => 'some'], - 'f2' => ['treatment' => 'off', 'config' => null], - ], true); - $this->assertEquals(null, $c->get('key', 'f1', null)); - $this->assertEquals(null, $c->get('key', 'f2', ['a' => 1])); - $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, false)); - $this->assertEquals(null, $c->getByFlagSets(['f1', 'f2'], 'key', null, true)); + $c->setFeaturesForFlagSets(['s1', 's2'], ['f1', 'f2']); + $this->assertEquals(null, $c->getFeaturesByFlagSets(['s1', 's2'])); + $this->assertEquals(null, $c->getFeaturesByFlagSets(['s2', 's1'])); + $this->assertEquals(null, $c->getFeaturesByFlagSets(['s1'])); } } From 91b2a6d781a137ba3edfe96fd04a2247fe36e2a8 Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Mon, 15 Jan 2024 14:13:42 -0300 Subject: [PATCH 5/5] removed unused assert --- tests/Utils/EvalCache/CacheImplTest.php | 1 - tests/Utils/EvalCache/NoCacheTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/Utils/EvalCache/CacheImplTest.php b/tests/Utils/EvalCache/CacheImplTest.php index 47e0e3d..05888fd 100644 --- a/tests/Utils/EvalCache/CacheImplTest.php +++ b/tests/Utils/EvalCache/CacheImplTest.php @@ -83,6 +83,5 @@ public function testByFlagSets() $c->setFeaturesForFlagSets(['s2', 's1'], ['f1', 'f2']); $this->assertEquals(['f1', 'f2'], $c->getFeaturesByFlagSets(['s1', 's2'])); $this->assertEquals(['f1', 'f2'], $c->getFeaturesByFlagSets(['s2', 's1'])); - $this->assertEquals(null, $c->getFeaturesByFlagSets(['s2'])); } } diff --git a/tests/Utils/EvalCache/NoCacheTest.php b/tests/Utils/EvalCache/NoCacheTest.php index 88593ca..c1ff696 100644 --- a/tests/Utils/EvalCache/NoCacheTest.php +++ b/tests/Utils/EvalCache/NoCacheTest.php @@ -43,6 +43,5 @@ public function testNoCache() $c->setFeaturesForFlagSets(['s1', 's2'], ['f1', 'f2']); $this->assertEquals(null, $c->getFeaturesByFlagSets(['s1', 's2'])); $this->assertEquals(null, $c->getFeaturesByFlagSets(['s2', 's1'])); - $this->assertEquals(null, $c->getFeaturesByFlagSets(['s1'])); } }