Skip to content

Commit dd7f268

Browse files
authored
Merge pull request #30 from launchdarkly/eb/ch24369/all-flags-caching
cache flag data in allFlags
2 parents 8391618 + 15720d0 commit dd7f268

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/LaunchDarkly/LDClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,17 @@ public function allFlagsState($user, $options = array())
350350
return new FeatureFlagsState(false);
351351
}
352352

353+
$preloadedRequester = new PreloadedFeatureRequester($this->_featureRequester, $flags);
354+
// This saves us from doing repeated queries for prerequisite flags during evaluation
355+
353356
$state = new FeatureFlagsState(true);
354357
$clientOnly = isset($options['clientSideOnly']) && $options['clientSideOnly'];
355358
$withReasons = isset($options['withReasons']) && $options['withReasons'];
356359
foreach ($flags as $key => $flag) {
357360
if ($clientOnly && !$flag->isClientSide()) {
358361
continue;
359362
}
360-
$result = $flag->evaluate($user, $this->_featureRequester);
363+
$result = $flag->evaluate($user, $preloadedRequester);
361364
$state->addFlag($flag, $result->getDetail(), $withReasons);
362365
}
363366
return $state;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace LaunchDarkly;
3+
4+
class PreloadedFeatureRequester implements FeatureRequester
5+
{
6+
/** @var FeatureRequester */
7+
private $_baseRequester;
8+
9+
/** @var array */
10+
private $_knownFeatures;
11+
12+
public function __construct($baseRequester, $knownFeatures)
13+
{
14+
$this->_baseRequester = $baseRequester;
15+
$this->_knownFeatures = $knownFeatures;
16+
}
17+
18+
/**
19+
* Gets feature data from cached values
20+
*
21+
* @param $key string feature key
22+
* @return FeatureFlag|null The decoded FeatureFlag, or null if missing
23+
*/
24+
public function getFeature($key)
25+
{
26+
if (isset($this->_knownFeatures[$key])) {
27+
return $this->_knownFeatures[$key];
28+
}
29+
return null;
30+
}
31+
32+
/**
33+
* Gets segment data from the regular feature requester
34+
*
35+
* @param $key string segment key
36+
* @return Segment|null The decoded Segment, or null if missing
37+
*/
38+
public function getSegment($key)
39+
{
40+
return $this->_baseRequester->getSegment($key);
41+
}
42+
43+
/**
44+
* Gets all features from cached values
45+
*
46+
* @return array()|null The decoded FeatureFlags, or null if missing
47+
*/
48+
public function getAllFeatures()
49+
{
50+
return $this->_knownFeatures;
51+
}
52+
}

0 commit comments

Comments
 (0)