From 21bb4371cc343debdf16465db0a9cbd712d03a1c Mon Sep 17 00:00:00 2001 From: farhatahmad Date: Tue, 20 Aug 2019 16:50:40 -0400 Subject: [PATCH] Cached request to provider to increase application performance --- app/controllers/admins_controller.rb | 8 ++++++++ app/views/admins/components/_settings.html.erb | 14 ++++++++++++-- config/locales/en.yml | 4 ++++ config/routes.rb | 1 + lib/bbb_api.rb | 7 +++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb index d133ba9c33..b5ea4e4de0 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admins_controller.rb @@ -176,6 +176,14 @@ def default_recording_visibility } end + # POST /admins/clear_cache + def clear_cache + Rails.cache.delete("#{@user_domain}/getUser") + Rails.cache.delete("#{@user_domain}/getUserGreenlightCredentials") + + redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") } + end + # ROLES # GET /admins/roles diff --git a/app/views/admins/components/_settings.html.erb b/app/views/admins/components/_settings.html.erb index 98a4ed8ac2..c90d7862e2 100644 --- a/app/views/admins/components/_settings.html.erb +++ b/app/views/admins/components/_settings.html.erb @@ -120,7 +120,7 @@ -
+
@@ -154,4 +154,14 @@
-
+ <% if current_user.has_role? :super_admin%> +
+
+
+ + + <%= button_to t("administrator.site_settings.cache.button"), admin_clear_cache_path, class: "btn btn-primary" %> +
+
+
+ <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 7298b3ea3d..ce9dee196d 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -42,6 +42,10 @@ en: info: Change the branding image that appears in the top left corner placeholder: Image Url... title: Branding Image + cache: + info: Clears the stored provider cache which forces a new request for the updated info + title: Clear Provider Cache + button: Clear Cache color: info: Changing the Regular Color will change both Lighten and Darken. Lighten and Darken can then be changed individually title: Primary Color diff --git a/config/routes.rb b/config/routes.rb index 7406a11d86..49b80ce347 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,7 @@ get '/reset', to: 'admins#reset', as: :admin_reset post '/room_limit', to: 'admins#room_limit', as: :admin_room_limit post '/default_recording_visibility', to: 'admins#default_recording_visibility', as: :admin_recording_visibility + post '/clear_cache', to: 'admins#clear_cache', as: :admin_clear_cache get '/roles', to: 'admins#roles', as: :admin_roles post '/role', to: 'admins#new_role', as: :admin_new_role patch 'roles/order', to: 'admins#change_role_order', as: :admin_roles_order diff --git a/lib/bbb_api.rb b/lib/bbb_api.rb index b76b3c8147..1b2f0c4959 100644 --- a/lib/bbb_api.rb +++ b/lib/bbb_api.rb @@ -27,6 +27,9 @@ def retrieve_provider_info(provider, api = 'api', route = 'getUser') # Include Omniauth accounts under the Greenlight provider. raise "Provider not included." if !provider || provider.empty? + cached_provider = Rails.cache.fetch("#{provider}/#{route}") + return cached_provider unless cached_provider.nil? + # Build the URI. uri = encode_bbb_url( Rails.configuration.loadbalancer_endpoint + api + '/', @@ -48,6 +51,10 @@ def retrieve_provider_info(provider, api = 'api', route = 'getUser') raise doc['message'] unless response.is_a?(Net::HTTPSuccess) # Return the user credentials if the request succeeded on the loadbalancer. + Rails.cache.fetch("#{provider}/#{route}", expires_in: 1.hours) do + doc['user'] + end + return doc['user'] if doc['returncode'] == 'SUCCESS' raise "User with provider #{provider} does not exist." if doc['messageKey'] == 'noSuchUser'