Class: Togul::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/togul/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.

Parameters:



10
11
12
13
14
# File 'lib/togul/client.rb', line 10

def initialize(config)
  @config = config
  @cache = Cache.new(ttl: config.cache_ttl)
  @stream_client = nil
end

Instance Method Details

#evaluate(key, context = {}) ⇒ Togul::EvaluateResult

Evaluate a feature flag and return the result mirroring the API response.

Parameters:

  • key (String)

    Flag key

  • context (Hash<String, String>) (defaults to: {})

    User/request context

Returns:



21
22
23
24
25
26
27
28
29
30
# File 'lib/togul/client.rb', line 21

def evaluate(key, context = {})
  cache_key = build_cache_key(key, context)

  cached = @cache.get(cache_key)
  return cached unless cached.nil?

  result = fetch_evaluation(key, context)
  @cache.set(cache_key, result)
  result
end

#invalidate_cacheObject

Clear all cached flag values.



33
34
35
# File 'lib/togul/client.rb', line 33

def invalidate_cache
  @cache.flush
end

#invalidate_flag(key) ⇒ Object

Clear a specific flag from cache.



38
39
40
# File 'lib/togul/client.rb', line 38

def invalidate_flag(key)
  @cache.invalidate_flag(key)
end

#on_cache_invalidated(&block) ⇒ Object

Register a listener for cache invalidation events.



48
49
50
# File 'lib/togul/client.rb', line 48

def on_cache_invalidated(&block)
  stream.on_cache_invalidated(&block)
end

#streamObject

Start SSE stream for real-time cache invalidation.



43
44
45
# File 'lib/togul/client.rb', line 43

def stream
  @stream_client ||= StreamClient.new(@config, @cache)
end