Skip to content

Commit c436c00

Browse files
feat: Added caching for optimizelyConfig object (#235)
* added caching. unit tests pending * added unit test * updated copyright
1 parent b7b450b commit c436c00

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

lib/optimizely.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2016-2019, Optimizely and contributors
4+
# Copyright 2016-2020, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -566,8 +566,13 @@ def get_optimizely_config
566566
return nil
567567
end
568568

569-
optimizely_config = OptimizelyConfig.new(project_config)
570-
optimizely_config.config
569+
# config_manager might not contain optimizely_config if its supplied by the consumer
570+
# Generating a new OptimizelyConfig object in this case as a fallback
571+
if @config_manager.respond_to?(:optimizely_config)
572+
@config_manager.optimizely_config
573+
else
574+
OptimizelyConfig.new(project_config).config
575+
end
571576
end
572577

573578
private

lib/optimizely/config_manager/http_project_config_manager.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2019, Optimizely and contributors
4+
# Copyright 2019-2020, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
require_relative '../logger'
2323
require_relative '../notification_center'
2424
require_relative '../project_config'
25+
require_relative '../optimizely_config'
2526
require_relative 'project_config_manager'
2627
require_relative 'async_scheduler'
2728
require 'httparty'
@@ -30,7 +31,7 @@ module Optimizely
3031
class HTTPProjectConfigManager < ProjectConfigManager
3132
# Config manager that polls for the datafile and updated ProjectConfig based on an update interval.
3233

33-
attr_reader :stopped
34+
attr_reader :stopped, :optimizely_config
3435

3536
# Initialize config manager. One of sdk_key or url has to be set to be able to use.
3637
#
@@ -73,6 +74,7 @@ def initialize(
7374
@skip_json_validation = skip_json_validation
7475
@notification_center = notification_center.is_a?(Optimizely::NotificationCenter) ? notification_center : NotificationCenter.new(@logger, @error_handler)
7576
@config = datafile.nil? ? nil : DatafileProjectConfig.create(datafile, @logger, @error_handler, @skip_json_validation)
77+
@optimizely_config = @config.nil? ? nil : OptimizelyConfig.new(@config).config
7678
@mutex = Mutex.new
7779
@resource = ConditionVariable.new
7880
@async_scheduler = AsyncScheduler.new(method(:fetch_datafile_config), @polling_interval, auto_update, @logger)
@@ -192,6 +194,7 @@ def set_config(config)
192194
end
193195

194196
@config = config
197+
@optimizely_config = OptimizelyConfig.new(config).config
195198

196199
@notification_center.send_notifications(NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE])
197200

lib/optimizely/config_manager/static_project_config_manager.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2019, Optimizely and contributors
4+
# Copyright 2019-2020, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -17,11 +17,13 @@
1717
#
1818

1919
require_relative '../config/datafile_project_config'
20+
require_relative '../optimizely_config'
2021
require_relative 'project_config_manager'
22+
2123
module Optimizely
2224
class StaticProjectConfigManager < ProjectConfigManager
2325
# Implementation of ProjectConfigManager interface.
24-
attr_reader :config
26+
attr_reader :config, :optimizely_config
2527

2628
def initialize(datafile, logger, error_handler, skip_json_validation)
2729
# Looks up and sets datafile and config based on response body.
@@ -38,6 +40,8 @@ def initialize(datafile, logger, error_handler, skip_json_validation)
3840
error_handler,
3941
skip_json_validation
4042
)
43+
44+
@optimizely_config = @config.nil? ? nil : OptimizelyConfig.new(@config).config
4145
end
4246
end
4347
end

spec/config_manager/http_project_config_manager_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2019, Optimizely and contributors
4+
# Copyright 2019-2020, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -423,4 +423,17 @@
423423
expect(spy_logger).to have_received(:log).once.with(Logger::DEBUG, "Blocking timeout '999999999999999999' has invalid range. Defaulting to 15 seconds.")
424424
end
425425
end
426+
427+
describe 'optimizely_config' do
428+
it 'optimizely_config object updates correctly when new config is recieved' do
429+
@http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
430+
sdk_key: 'valid_sdk_key',
431+
datafile: config_body_JSON,
432+
polling_interval: 0.1
433+
)
434+
expect(@http_project_config_manager.optimizely_config['revision']).to eq('42')
435+
sleep 0.3
436+
expect(@http_project_config_manager.optimizely_config['revision']).to eq('81')
437+
end
438+
end
426439
end

0 commit comments

Comments
 (0)