-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
request-scoped evaluation cache implementation
- Loading branch information
1 parent
8797594
commit 0129531
Showing
23 changed files
with
575 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Utils\EvalCache; | ||
|
||
interface Cache | ||
{ | ||
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 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Utils\EvalCache; | ||
|
||
class CacheImpl implements Cache | ||
{ | ||
|
||
private /*array*/ $data; | ||
private /*InputHasher*/ $hasher; | ||
|
||
public function __construct(InputHasher $hasher) | ||
{ | ||
$this->data = []; | ||
$this->hasher = $hasher; | ||
} | ||
|
||
public function get(string $key, string $feature, ?array $attributes): ?string | ||
{ | ||
$entry = $this->_get($key, $feature, $attributes); | ||
return ($entry != null) ? $entry->getTreatment() : null; | ||
} | ||
|
||
public function getMany(string $key, array $features, ?array $attributes): array | ||
{ | ||
$result = []; | ||
foreach ($features as $feature) { | ||
$result[$feature] = $this->get($key, $feature, $attributes); | ||
} | ||
return $result; | ||
} | ||
|
||
public function getWithConfig(string $key, string $feature, ?array $attributes): ?array | ||
{ | ||
// if the entry exists but was previously fetched without config, it's returned as null, | ||
// so that it's properly fetched by `getTreatmentWithConfig` | ||
$entry = $this->_get($key, $feature, $attributes); | ||
return ($entry != null && $entry->hasConfig()) | ||
? ['treatment' => $entry->getTreatment(), 'config' => $entry->getConfig()] | ||
: null; | ||
} | ||
|
||
public function getManyWithConfig(string $key, array $features, ?array $attributes): array | ||
{ | ||
$result = []; | ||
foreach ($features as $feature) { | ||
$result[$feature] = $this->getWithConfig($key, $feature, $attributes); | ||
} | ||
return $result; | ||
} | ||
|
||
public function set(string $key, string $feature, ?array $attributes, string $treatment) | ||
{ | ||
$h = $this->hasher->hashInput($key, $feature, $attributes); | ||
$this->data[$h] = new Entry($treatment, false); | ||
} | ||
|
||
public function setMany(string $key, ?array $attributes, array $results) | ||
{ | ||
foreach ($results as $feature => $treatment) { | ||
$this->set($key, $feature, $attributes, $treatment); | ||
} | ||
} | ||
|
||
public function setWithConfig(string $key, string $feature, ?array $attributes, string $treatment, ?string $config) | ||
{ | ||
$h = $this->hasher->hashInput($key, $feature, $attributes); | ||
$this->data[$h] = new Entry($treatment, true, $config); | ||
} | ||
|
||
public function setManyWithConfig(string $key, ?array $attributes, array $results) | ||
{ | ||
foreach ($results as $feature => $result) { | ||
$this->setWithConfig($key, $feature, $attributes, $result['treatment'], $result['config']); | ||
} | ||
} | ||
|
||
private function _get(string $key, string $feature, ?array $attributes): ?Entry | ||
{ | ||
$h = $this->hasher->hashInput($key, $feature, $attributes); | ||
return $this->data[$h] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Utils\EvalCache; | ||
|
||
class Entry | ||
{ | ||
private /*string*/ $treatment; | ||
private /*string?*/ $config; | ||
private /*bool*/ $hascfg; | ||
|
||
public function __construct(string $treatment, bool $hasConfig = false, ?string $config = null) | ||
{ | ||
$this->treatment = $treatment; | ||
$this->config = $config; | ||
$this->hascfg = $hasConfig; | ||
} | ||
|
||
public function getTreatment(): string | ||
{ | ||
return $this->treatment; | ||
} | ||
|
||
public function getConfig(): ?string | ||
{ | ||
return $this->config; | ||
} | ||
|
||
public function hasConfig(): bool | ||
{ | ||
return $this->hascfg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Utils\EvalCache; | ||
|
||
interface InputHasher | ||
{ | ||
public function hashInput(string $key, string $feature, ?array $attributes = null): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Utils\EvalCache; | ||
|
||
class KeyAttributeCRC32Hasher implements InputHasher | ||
{ | ||
public function hashInput(string $key, string $feature, ?array $attributes = null): string | ||
{ | ||
// based on https://grechin.org/2021/04/06/php-json-encode-vs-serialize-performance-comparison.html | ||
// php `serialize` is slower for encoding but faster for decoding. | ||
// Since this we never decode here (we're just serializing for hashing purposes), | ||
// it makes sense to go with JSON | ||
$prefix = $key . "::" . $feature; | ||
return is_null($attributes) ? $prefix : $prefix . "::" . crc32(json_encode($attributes)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Utils\EvalCache; | ||
|
||
class KeyOnlyHasher implements InputHasher | ||
{ | ||
public function hashInput(string $key, string $feature, ?array $attributes = null): string | ||
{ | ||
return $key . "::" . $feature; | ||
} | ||
} |
Oops, something went wrong.