Skip to content

Commit c523a17

Browse files
committed
Removed cache from DI
1 parent 9c74c41 commit c523a17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+574
-411
lines changed

src/SplitIO/Component/Cache/EventsCache.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,30 @@
33

44
use SplitIO\Component\Common\Di;
55
use SplitIO\Sdk\Events\EventQueueMessage;
6+
use SplitIO\Component\Cache\Pool;
67

78
class EventsCache
89
{
910
const KEY_EVENTS_LIST = "SPLITIO.events";
1011

11-
public static function addEvent(EventQueueMessage $message)
12+
/**
13+
* @var \SplitIO\Component\Cache\Pool
14+
*/
15+
private $cache;
16+
17+
/**
18+
* @param \SplitIO\Component\Cache\Pool $cache
19+
*/
20+
public function __construct(Pool $cache) {
21+
$this->cache = $cache;
22+
}
23+
24+
public function addEvent(EventQueueMessage $message)
1225
{
1326
$queueJSONmessage = json_encode($message->toArray());
1427

28+
// @TODO REMOVE LOGGER DI
1529
Di::getLogger()->debug("Adding event item into queue: ". $queueJSONmessage);
16-
return (Di::getCache()->rightPushInList(self::KEY_EVENTS_LIST, $queueJSONmessage) > 0);
30+
return ($this->cache->rightPushInList(self::KEY_EVENTS_LIST, $queueJSONmessage) > 0);
1731
}
1832
}

src/SplitIO/Component/Cache/ImpressionCache.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@
44
use SplitIO\Component\Common\Di;
55
use SplitIO\Component\Cache\KeyFactory;
66
use SplitIO\Sdk\QueueMetadataMessage;
7+
use SplitIO\Component\Cache\Pool;
78

89
class ImpressionCache
910
{
1011
const IMPRESSIONS_QUEUE_KEY = "SPLITIO.impressions";
1112
const IMPRESSION_KEY_DEFAULT_TTL = 3600;
1213

14+
/**
15+
* @var \SplitIO\Component\Cache\Pool
16+
*/
17+
private $cache;
18+
19+
/**
20+
* @param \SplitIO\Component\Cache\Pool $cache
21+
*/
22+
public function __construct(Pool $cache) {
23+
$this->cache = $cache;
24+
}
25+
1326
public function logImpressions($impressions, QueueMetadataMessage $metadata)
1427
{
1528
$toStore = array_map(
@@ -30,10 +43,11 @@ function ($imp) use ($metadata) {
3043
$impressions
3144
);
3245

46+
// @TODO REMOVE LOGGER DI
3347
Di::getLogger()->debug("Adding impressions into queue: ". implode(",", $toStore));
34-
$count = Di::getCache()->rightPushInList(self::IMPRESSIONS_QUEUE_KEY, $toStore);
48+
$count = $this->cache->rightPushInList(self::IMPRESSIONS_QUEUE_KEY, $toStore);
3549
if ($count == count($impressions)) {
36-
Di::getCache()->expireKey(self::IMPRESSIONS_QUEUE_KEY, self::IMPRESSION_KEY_DEFAULT_TTL);
50+
$this->cache->expireKey(self::IMPRESSIONS_QUEUE_KEY, self::IMPRESSION_KEY_DEFAULT_TTL);
3751
}
3852
return ($count >= count($impressions));
3953
}

src/SplitIO/Component/Cache/SegmentCache.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ class SegmentCache implements SegmentCacheInterface
99

1010
const KEY_TILL_CACHED_ITEM = 'SPLITIO.segment.{segment_name}.till';
1111

12+
/**
13+
* @var \SplitIO\Component\Cache\Pool
14+
*/
15+
private $cache;
16+
17+
/**
18+
* @param \SplitIO\Component\Cache\Pool $cache
19+
*/
20+
public function __construct(Pool $cache) {
21+
$this->cache = $cache;
22+
}
23+
1224
private static function getCacheKeyForSegmentData($segmentName)
1325
{
1426
return str_replace('{segmentName}', $segmentName, self::KEY_SEGMENT_DATA);
@@ -27,7 +39,7 @@ private static function getCacheKeyForSinceParameter($segmentName)
2739
public function isInSegment($segmentName, $key)
2840
{
2941
$segmentDataKey = self::getCacheKeyForSegmentData($segmentName);
30-
return Di::getCache()->isItemOnList($segmentDataKey, $key);
42+
return $this->cache->isItemOnList($segmentDataKey, $key);
3143
}
3244

3345
/**
@@ -36,7 +48,7 @@ public function isInSegment($segmentName, $key)
3648
*/
3749
public function getChangeNumber($segmentName)
3850
{
39-
$since = Di::getCache()->get(self::getCacheKeyForSinceParameter($segmentName));
51+
$since = $this->cache->get(self::getCacheKeyForSinceParameter($segmentName));
4052
// empty check for nullable value
4153
return (empty($since)) ? -1 : $since;
4254
}

src/SplitIO/Component/Cache/SplitCache.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ class SplitCache implements SplitCacheInterface
1111

1212
const KEY_TRAFFIC_TYPE_CACHED = 'SPLITIO.trafficType.{trafficTypeName}';
1313

14+
/**
15+
* @var \SplitIO\Component\Cache\Pool
16+
*/
17+
private $cache;
18+
19+
/**
20+
* @param \SplitIO\Component\Cache\Pool $cache
21+
*/
22+
public function __construct(Pool $cache) {
23+
$this->cache = $cache;
24+
}
25+
1426
private static function getCacheKeyForSinceParameter()
1527
{
1628
return self::KEY_TILL_CACHED_ITEM;
@@ -37,7 +49,7 @@ private static function getSplitNameFromCacheKey($key)
3749
*/
3850
public function getChangeNumber()
3951
{
40-
$since = Di::getCache()->get(self::getCacheKeyForSinceParameter());
52+
$since = $this->cache->get(self::getCacheKeyForSinceParameter());
4153
// empty check for nullable value
4254
return (empty($since)) ? -1 : $since;
4355
}
@@ -48,8 +60,7 @@ public function getChangeNumber()
4860
*/
4961
public function getSplit($splitName)
5062
{
51-
$cache = Di::getCache();
52-
return $cache->get(self::getCacheKeyForSplit($splitName));
63+
return $this->cache->get(self::getCacheKeyForSplit($splitName));
5364
}
5465

5566
/**
@@ -58,8 +69,7 @@ public function getSplit($splitName)
5869
*/
5970
public function getSplits($splitNames)
6071
{
61-
$cache = Di::getCache();
62-
$cacheItems = $cache->fetchMany(array_map('self::getCacheKeyForSplit', $splitNames));
72+
$cacheItems = $this->cache->fetchMany(array_map('self::getCacheKeyForSplit', $splitNames));
6373
$toReturn = array();
6474
foreach ($cacheItems as $key => $value) {
6575
$toReturn[self::getSplitNameFromCacheKey($key)] = $value;
@@ -72,8 +82,7 @@ public function getSplits($splitNames)
7282
*/
7383
public function getSplitNames()
7484
{
75-
$cache = Di::getCache();
76-
$splitKeys = $cache->getKeys(self::getCacheKeySearchPattern());
85+
$splitKeys = $this->cache->getKeys(self::getCacheKeySearchPattern());
7786
return array_map('self::getSplitNameFromCacheKey', $splitKeys);
7887
}
7988

@@ -97,9 +106,7 @@ private static function getCacheKeyForTrafficType($trafficType)
97106
*/
98107
public function trafficTypeExists($trafficType)
99108
{
100-
$cache = Di::getCache();
101-
102-
$count = $cache->get(self::getCacheKeyForTrafficType($trafficType));
109+
$count = $this->cache->get(self::getCacheKeyForTrafficType($trafficType));
103110
// empty check for nullable value
104111
return (empty($count) || $count < 1) ? false : true;
105112
}

src/SplitIO/Component/Common/Di.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ class Di
1212
{
1313
const KEY_LOG = 'SPLIT-LOGGER';
1414

15-
const KEY_CACHE = 'SPLIT-CACHE';
16-
17-
const KEY_SPLIT_CLIENT = 'SPLIT-CLIENT';
18-
19-
const KEY_SPLIT_CLIENT_CONFIG = 'SPLIT-CLIENT-CONFIG';
20-
21-
const KEY_SPLIT_SDK_CONFIG = 'SPLIT-SDK-CONFIG';
22-
23-
const KEY_EVALUATOR = 'EVALUATOR';
24-
2515
const KEY_FACTORY_TRACKER = 'FACTORY-TRACKER';
2616

2717
/**
@@ -131,29 +121,4 @@ public static function getLogger()
131121
return self::get(self::KEY_LOG);
132122
}
133123

134-
/**
135-
* @param \SplitIO\Component\Cache\Pool $cachePool
136-
*/
137-
public static function setCache(Pool $cachePool)
138-
{
139-
self::set(self::KEY_CACHE, $cachePool);
140-
}
141-
142-
/**
143-
* @return null|\SplitIO\Component\Cache\Pool
144-
*/
145-
public static function getCache()
146-
{
147-
return self::get(self::KEY_CACHE);
148-
}
149-
150-
public static function setEvaluator(\SplitIO\Sdk\Evaluator $evaluator)
151-
{
152-
self::set(self::KEY_EVALUATOR, $evaluator);
153-
}
154-
155-
public static function getEvaluator()
156-
{
157-
return self::get(self::KEY_EVALUATOR);
158-
}
159124
}

src/SplitIO/Component/Common/ServiceProvider.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,4 @@ public static function registerLogger($logger)
77
{
88
Di::setLogger($logger);
99
}
10-
11-
public static function registerCache(\SplitIO\Component\Cache\Pool $cache)
12-
{
13-
Di::setCache($cache);
14-
}
1510
}

src/SplitIO/Component/Initialization/CacheTrait.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/SplitIO/Engine.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class Engine
1919
* @param $bucketingKey
2020
* @param SplitGrammar $split
2121
* @param array|null $attributes
22+
* @param array|null $context
2223
* @return array
2324
*/
24-
public static function getTreatment($matchingKey, $bucketingKey, SplitGrammar $split, array $attributes = null)
25+
public static function getTreatment($matchingKey, $bucketingKey, SplitGrammar $split, array $attributes = null, array $context = null)
2526
{
2627
if ($bucketingKey === null) {
2728
$bucketingKey = $matchingKey;
@@ -38,7 +39,7 @@ public static function getTreatment($matchingKey, $bucketingKey, SplitGrammar $s
3839
foreach ($conditions as $condition) {
3940
if (!$inRollOut && $condition->getConditionType() == ConditionTypeEnum::ROLLOUT) {
4041
if ($split->getTrafficAllocation() < 100) {
41-
$bucket = Di::get('splitter')->getBucket(
42+
$bucket = Splitter::getBucket(
4243
$split->getAlgo(),
4344
$bucketingKey,
4445
$split->getTrafficAllocationSeed()
@@ -51,8 +52,8 @@ public static function getTreatment($matchingKey, $bucketingKey, SplitGrammar $s
5152
$inRollOut = true;
5253
}
5354
}
54-
if ($condition->match($matchingKey, $attributes, $bucketingKey)) {
55-
$result[self::EVALUATION_RESULT_TREATMENT] = Di::get('splitter')->getTreatment(
55+
if ($condition->match($matchingKey, $attributes, $bucketingKey, $context)) {
56+
$result[self::EVALUATION_RESULT_TREATMENT] = Splitter::getTreatment(
5657
$bucketingKey,
5758
$split->getSeed(),
5859
$condition->getPartitions(),

src/SplitIO/Engine/Splitter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Splitter
1313
* @param long $seed
1414
* @return int
1515
*/
16-
public function getBucket($algo, $key, $seed)
16+
public static function getBucket($algo, $key, $seed)
1717
{
1818
$hashFactory = HashFactory::getHashAlgorithm($algo);
1919
$hash = $hashFactory->getHash($key, $seed);
@@ -28,7 +28,7 @@ public function getBucket($algo, $key, $seed)
2828
* @param HashAlgorithmEnum $algo
2929
* @return null|string
3030
*/
31-
public function getTreatment($key, $seed, $partitions, $algo)
31+
public static function getTreatment($key, $seed, $partitions, $algo)
3232
{
3333
$logMsg = "Splitter evaluating partitions ... \n
3434
Bucketing Key: $key \n

src/SplitIO/Grammar/Condition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(array $condition)
6666
* @param array|null $attributes
6767
* @return bool
6868
*/
69-
public function match($key, array $attributes = null, $bucketingKey = null)
69+
public function match($key, array $attributes = null, $bucketingKey = null, array $context = null)
7070
{
7171
$eval = array();
7272
foreach ($this->matcherGroup as $matcher) {
@@ -76,7 +76,7 @@ public function match($key, array $attributes = null, $bucketingKey = null)
7676
if (!$matcher->hasAttribute()) {
7777
// scenario 1: no attr in matcher
7878
// e.g. if user is in segment all then split 100:on
79-
$_evaluation = $matcher->evaluate($key);
79+
$_evaluation = $matcher->evaluate($key, $context);
8080
} else {
8181
// scenario 2: attribute provided but no attribute value provided. Matcher does not match
8282
// e.g. if user.age is >= 10 then split 100:on
@@ -96,7 +96,7 @@ public function match($key, array $attributes = null, $bucketingKey = null)
9696
$printableAttributes = is_array($attributes) ? implode($attributes) : $attributes;
9797
SplitApp::logger()->info("Evaluating on IN_SPLIT_TREATMENT the KEY $printable");
9898
SplitApp::logger()->info("with the following attributes: $printableAttributes");
99-
$_evaluation = $matcher->evalKey($key, $attributes, $bucketingKey);
99+
$_evaluation = $matcher->evalKey($key, $attributes, $bucketingKey, $context);
100100
$eval[] = ($matcher->isNegate()) ? NotFactor::evaluate($_evaluation) : $_evaluation ;
101101
} else {
102102
//Throwing catchable exception the SDK client will return CONTROL

src/SplitIO/Grammar/Condition/Matcher/AbstractMatcher.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ protected function __construct($type, $negate = false, $attribute = null)
2222
$this->attribute = $attribute;
2323
}
2424

25-
public function evaluate($key)
25+
public function evaluate($key, array $context = null)
2626
{
2727
$printable = is_array($key) ? implode($key) : $key;
2828
SplitApp::logger()->info("Evaluating on {$this->type} the KEY $printable");
2929

30-
return $this->evalKey($key);
30+
return $this->evalKey($key, $context);
3131
}
3232

3333
public function isNegate()
@@ -45,5 +45,5 @@ public function getAttribute()
4545
return $this->attribute;
4646
}
4747

48-
abstract protected function evalKey($key);
48+
abstract protected function evalKey($key, array $context);
4949
}

src/SplitIO/Grammar/Condition/Matcher/All.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __construct($negate = false, $attribute = null)
1212
parent::__construct(Matcher::ALL_KEYS, $negate, $attribute);
1313
}
1414

15-
protected function evalKey($key)
15+
protected function evalKey($key, array $context = null)
1616
{
1717
SplitApp::logger()->info("Comparing: ALL_KEYS - $key");
1818
SplitApp::logger()->info("User found: $key");

src/SplitIO/Grammar/Condition/Matcher/Between.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct($data, $negate = false, $attribute = null)
2323
* @param $key
2424
* @return bool
2525
*/
26-
protected function evalKey($key)
26+
protected function evalKey($key, array $context = null)
2727
{
2828
if (!is_long($key)) {
2929
return false;

src/SplitIO/Grammar/Condition/Matcher/ContainsAllOfSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct($data, $negate = false, $attribute = null)
1616
$this->set = Set::fromArray($data);
1717
}
1818

19-
protected function evalKey($key)
19+
protected function evalKey($key, array $context = null)
2020
{
2121
if (!is_array($key)) {
2222
return false;

0 commit comments

Comments
 (0)