Class: ConfigCat::LazyLoadingCachePolicy

Inherits:
CachePolicy show all
Defined in:
lib/configcat/lazyloadingcachepolicy.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_fetcher, config_cache, cache_key, cache_time_to_live_seconds = 60) ⇒ LazyLoadingCachePolicy

Returns a new instance of LazyLoadingCachePolicy.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/configcat/lazyloadingcachepolicy.rb', line 9

def initialize(config_fetcher, config_cache, cache_key, cache_time_to_live_seconds=60)
  if cache_time_to_live_seconds < 1
    cache_time_to_live_seconds = 1
  end
  @_config_fetcher = config_fetcher
  @_config_cache = config_cache
  @_cache_key = cache_key
  @_cache_time_to_live = cache_time_to_live_seconds
  @_lock = Concurrent::ReadWriteLock.new()
  @_last_updated = nil
end

Instance Method Details

#force_refreshObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/configcat/lazyloadingcachepolicy.rb', line 44

def force_refresh()
  begin
    configuration_response = @_config_fetcher.get_configuration_json()
    if configuration_response.is_fetched()
      configuration = configuration_response.json()
      begin
        @_lock.acquire_write_lock()
        @_config_cache.set(@_cache_key, configuration)
        @_last_updated = Time.now.utc
      ensure
        @_lock.release_write_lock()
      end
    end
  rescue StandardError => e
    ConfigCat.logger.error("Double-check your SDK Key at https://app.configcat.com/sdkkey.")
    ConfigCat.logger.error "threw exception #{e.class}:'#{e}'"
    ConfigCat.logger.error "stacktrace: #{e.backtrace}"
  end
end

#getObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/configcat/lazyloadingcachepolicy.rb', line 21

def get()
  begin
    @_lock.acquire_read_lock()
    utc_now = Time.now.utc
    if !@_last_updated.equal?(nil) && (@_last_updated + @_cache_time_to_live > utc_now)
      config = @_config_cache.get(@_cache_key)
      if !config.equal?(nil)
        return config
      end
    end
  ensure
    @_lock.release_read_lock()
  end
  force_refresh()
  begin
    @_lock.acquire_read_lock()
    config = @_config_cache.get(@_cache_key)
    return config
  ensure
    @_lock.release_read_lock()
  end
end

#stopObject



64
65
# File 'lib/configcat/lazyloadingcachepolicy.rb', line 64

def stop()
end