-
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.
Merge pull request #14 from splitio/enh/evalcache
request-scoped evaluation cache implementation
- Loading branch information
Showing
31 changed files
with
817 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Config; | ||
|
||
use SplitIO\ThinSdk\Utils\EvalCache\InputHasher; | ||
|
||
|
||
class EvaluationCache | ||
{ | ||
private /*?string*/ $type; | ||
private /*?InputHasher*/ $customHash; | ||
private /*string*/ $evictionPolicy; | ||
private /*int*/ $maxSize; | ||
|
||
private function __construct(string $type, ?InputHasher $customHash, string $evictionPolicy, int $maxSize) | ||
{ | ||
$this->type = $type; | ||
$this->customHash = $customHash; | ||
$this->evictionPolicy = $evictionPolicy; | ||
$this->maxSize = $maxSize; | ||
} | ||
|
||
public function type(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function customHash(): ?InputHasher | ||
{ | ||
return $this->customHash; | ||
} | ||
|
||
public function evictionPolicy(): string | ||
{ | ||
return $this->evictionPolicy; | ||
} | ||
|
||
public function maxSize(): int | ||
{ | ||
return $this->maxSize; | ||
} | ||
|
||
public static function fromArray(array $config): EvaluationCache | ||
{ | ||
$d = self::default(); | ||
return new EvaluationCache( | ||
$config['type'] ?? $d->type(), | ||
$config['customHash'] ?? $d->customHash(), | ||
$config['evictionPolicy'] ?? $d->evictionPolicy(), | ||
$config['maxSize'] ?? $d->maxSize(), | ||
); | ||
} | ||
|
||
public static function default(): EvaluationCache | ||
{ | ||
return new EvaluationCache('none', null, 'no-eviction', 1000); | ||
} | ||
} |
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,86 @@ | ||
<?php | ||
|
||
namespace SplitIO\ThinSdk\Utils\EvalCache; | ||
|
||
class CacheImpl implements Cache | ||
{ | ||
|
||
private /*array*/ $data; | ||
private /*InputHasher*/ $hasher; | ||
private /*EvictionPolicy*/ $evictionPolicy; | ||
|
||
public function __construct(InputHasher $hasher, EvictionPolicy $evictionPolicy) | ||
{ | ||
$this->data = []; | ||
$this->hasher = $hasher; | ||
$this->evictionPolicy = $evictionPolicy; | ||
} | ||
|
||
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); | ||
$this->evictionPolicy->postCacheInsertionHook($h, $this->data); | ||
} | ||
|
||
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); | ||
$this->evictionPolicy->postCacheInsertionHook($h, $this->data); | ||
} | ||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.