@@ -10,6 +10,7 @@ class LDDFeatureRequester implements FeatureRequester
1010 protected $ _sdkKey ;
1111 protected $ _options ;
1212 protected $ _features_key ;
13+ protected $ _segments_key ;
1314 /** @var LoggerInterface */
1415 private $ _logger ;
1516 /** @var ClientInterface */
@@ -33,6 +34,7 @@ public function __construct($baseUri, $sdkKey, $options)
3334 $ prefix = $ options ['redis_prefix ' ];
3435 }
3536 $ this ->_features_key = "$ prefix:features " ;
37+ $ this ->_segments_key = "$ prefix:segments " ;
3638 $ this ->_logger = $ options ['logger ' ];
3739
3840 if (isset ($ this ->_options ['predis_client ' ]) && $ this ->_options ['predis_client ' ] instanceof ClientInterface) {
@@ -56,21 +58,20 @@ protected function get_connection()
5658 "port " => $ this ->_options ['redis_port ' ]));
5759 }
5860
59-
6061 /**
6162 * Gets feature data from a likely cached store
6263 *
6364 * @param $key string feature key
6465 * @return FeatureFlag|null The decoded JSON feature data, or null if missing
6566 */
66- public function get ($ key )
67+ public function getFeature ($ key )
6768 {
68- $ raw = $ this ->get_from_cache ($ key );
69+ $ raw = $ this ->get_from_cache ($ this -> _features_key , $ key );
6970 if ($ raw === null ) {
7071 $ redis = $ this ->get_connection ();
7172 $ raw = $ redis ->hget ($ this ->_features_key , $ key );
7273 if ($ raw ) {
73- $ this ->store_in_cache ($ key , $ raw );
74+ $ this ->store_in_cache ($ this -> _features_key , $ key , $ raw );
7475 }
7576 }
7677 if ($ raw ) {
@@ -86,22 +87,53 @@ public function get($key)
8687 }
8788 }
8889
90+ /**
91+ * Gets segment data from a likely cached store
92+ *
93+ * @param $key string segment key
94+ * @return Segment|null The decoded JSON segment data, or null if missing
95+ */
96+ public function getSegment ($ key )
97+ {
98+ $ raw = $ this ->get_from_cache ($ this ->_segments_key , $ key );
99+ if ($ raw === null ) {
100+ $ redis = $ this ->get_connection ();
101+ $ raw = $ redis ->hget ($ this ->_features_key , $ key );
102+ if ($ raw ) {
103+ $ this ->store_in_cache ($ this ->_segments_key , $ key , $ raw );
104+ }
105+ }
106+ if ($ raw ) {
107+ $ segment = Segment::decode (json_decode ($ raw , true ));
108+ if ($ segment ->isDeleted ()) {
109+ $ this ->_logger ->warning ("LDDFeatureRequester: Attempted to get deleted segment with key: " . $ key );
110+ return null ;
111+ }
112+ return $ segment ;
113+ } else {
114+ $ this ->_logger ->warning ("LDDFeatureRequester: Attempted to get missing segment with key: " . $ key );
115+ return null ;
116+ }
117+ }
118+
89119 /**
90120 * Gets the value from local cache. No-op by default.
91- * @param $key string The feature key
92- * @return null|array The feature data or null if missing
121+ * @param $namespace string that denotes features or segments
122+ * @param $key string The feature or segment key
123+ * @return null|array The feature or segment data or null if missing
93124 */
94- protected function get_from_cache ($ key )
125+ protected function get_from_cache ($ namespace , $ key )
95126 {
96127 return null ;
97128 }
98129
99130 /**
100- * Stores the feature data into the local cache. No-op by default.
101- * @param $key string The feature key
102- * @param $val array The feature data
131+ * Stores the feature or segment data into the local cache. No-op by default.
132+ * @param $namespace string that denotes features or segments
133+ * @param $key string The feature or segment key
134+ * @param $val array The feature or segment data
103135 */
104- protected function store_in_cache ($ key , $ val )
136+ protected function store_in_cache ($ namespace , $ key , $ val )
105137 {
106138 }
107139
@@ -110,7 +142,7 @@ protected function store_in_cache($key, $val)
110142 *
111143 * @return array()|null The decoded FeatureFlags, or null if missing
112144 */
113- public function getAll ()
145+ public function getAllFeatures ()
114146 {
115147 $ redis = $ this ->get_connection ();
116148 $ raw = $ redis ->hgetall ($ this ->_features_key );
0 commit comments