Class: Growthbook::RedisResolver
- Inherits:
-
Object
- Object
- Growthbook::RedisResolver
- Defined in:
- lib/growthbook/redis_resolver.rb
Overview
A resolver for Context#add_remote_saved_groups, for remote saved groups loaded
into Redis by GrowthBook's loader: one SMEMBERS gb:member:<attribute>:<value>
per attribute value, sent in one pipeline when the client has pipelined.
Takes any client with smembers(key), such as the redis
gem's Redis; the SDK doesn't depend on one. Not loaded by require 'growthbook':
require 'growthbook/redis_resolver'
context.add_remote_saved_groups(Growthbook::RedisResolver.new(Redis.new))
A derived group checks its own attribute's value in its source group's sets:
grp_vip:parent_id looks up gb:member:<grp_vip's attribute>:<parent_id> and
matches when grp_vip comes back.
Instance Method Summary collapse
-
#call(attributes, groups) ⇒ Array<String>
The ids of the groups the user is in.
-
#initialize(redis) ⇒ RedisResolver
constructor
A new instance of RedisResolver.
Constructor Details
#initialize(redis) ⇒ RedisResolver
Returns a new instance of RedisResolver.
20 21 22 |
# File 'lib/growthbook/redis_resolver.rb', line 20 def initialize(redis) @redis = redis end |
Instance Method Details
#call(attributes, groups) ⇒ Array<String>
Returns The ids of the groups the user is in.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/growthbook/redis_resolver.rb', line 27 def call(attributes, groups) # For each group and value: the Redis key, and the id that means a match checks = [] groups.each do |group| source = group['valuesFrom'] ? groups.find { |g| g['id'] == group['valuesFrom'] } : group next if source.nil? lookup_values(attributes[group['attributeKey']]).each do |value| checks << [group['id'], "gb:member:#{source['attributeKey']}:#{value}", source['id']] end end keys = checks.map { |check| check[1] }.uniq members = keys.zip(smembers(keys)).to_h # Checking for the expected id, not any id in the set, also keeps keys that # collide when attributes or values contain ":" (`a:b` + `c` and `a` + `b:c`) # from mixing up groups. checks.select { |_id, key, match_id| members[key].map(&:to_s).include?(match_id) }.map(&:first).uniq end |