lib/featureswitches.rb in featureswitches-0.8.1 vs lib/featureswitches.rb in featureswitches-0.8.2

- old
+ new

@@ -13,11 +13,13 @@ @last_dirty_check = 0 @api = options[:api] ||= 'https://api.featureswitches.com/v1/' @cache = Cache.new - @dirty_check_thread = do_dirty_check + if @cache_timeout > 0 + @dirty_check_thread = do_dirty_check + end end def authenticate endpoint = 'authenticate' response = api_request(endpoint) @@ -25,17 +27,20 @@ return response end def sync endpoint = 'features' - response = api_request(endpoint) - features = response[:data]['features'] + if @cache_timeout > 0 + response = api_request(endpoint) - features.each do |feature| - feature['last_sync'] = Time.now.to_i - @cache[feature['feature_key']] = feature + features = response[:data]['features'] + + features.each do |feature| + feature['last_sync'] = Time.now.to_i + @cache[feature['feature_key']] = feature + end end end def is_enabled(feature_key, user_identifier=nil, default=false) feature = @cache[feature_key] @@ -89,11 +94,13 @@ if response[:success] feature = response[:data]['feature'] feature['last_sync'] = Time.now.to_i - @cache[feature['feature_key']] = feature + if @cache_timeout > 0 + @cache[feature['feature_key']] = feature + end return feature end return nil @@ -120,9 +127,13 @@ return feature['enabled'] end def cache_is_stale(feature) + if @cache_timeout == 0 + return true + end + cache_expiration = (Time.now.to_i - @cache_timeout) if feature['last_sync'] > cache_expiration and @last_dirty_check > cache_expiration return false end