From 8d48c3ce3de059819d0f6bfe1e152182228f0569 Mon Sep 17 00:00:00 2001 From: Fosco Marotto Date: Sat, 16 Jan 2021 13:52:11 -0500 Subject: [PATCH] Redis connection pooling updates --- app/controllers/admin/dashboard_controller.rb | 12 ++-- .../admin/trending_hashtags_controller.rb | 8 ++- .../api/v1/accounts/credentials_controller.rb | 22 ++++--- app/controllers/api/v1/expenses_controller.rb | 4 +- .../api/v1/popular_links_controller.rb | 62 ++++++++++--------- .../api/v1/trending_hashtags_controller.rb | 4 +- 6 files changed, 64 insertions(+), 48 deletions(-) diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb index 02358900..8317ceae 100644 --- a/app/controllers/admin/dashboard_controller.rb +++ b/app/controllers/admin/dashboard_controller.rb @@ -8,9 +8,11 @@ module Admin @statuses_count = "." #Status.count @pro_accounts_count = Account.where(is_pro: true).count @donor_accounts_count = Account.where(is_donor: true).count - @registrations_week = Redis.current.get("activity:accounts:local:#{current_week}") || 0 - @logins_week = Redis.current.pfcount("activity:logins:#{current_week}") - @interactions_week = Redis.current.get("activity:interactions:#{current_week}") || 0 + Redis.current.with do |conn| + @registrations_week = conn.get("activity:accounts:local:#{current_week}") || 0 + @logins_week = conn.pfcount("activity:logins:#{current_week}") + @interactions_week = conn.get("activity:interactions:#{current_week}") || 0 + end @single_user_mode = Rails.configuration.x.single_user_mode @search_enabled = Chewy.enabled? @version = GabSocial::Version.to_s @@ -35,7 +37,9 @@ module Admin end def redis_info - @redis_info ||= Redis.current.info + Redis.current.with do |conn| + @redis_info ||= conn.info + end end end end diff --git a/app/controllers/admin/trending_hashtags_controller.rb b/app/controllers/admin/trending_hashtags_controller.rb index 04f9aaf2..3110da2d 100644 --- a/app/controllers/admin/trending_hashtags_controller.rb +++ b/app/controllers/admin/trending_hashtags_controller.rb @@ -1,10 +1,14 @@ class Admin::TrendingHashtagsController < Admin::BaseController def index - @trending_hashtags = Redis.current.get("admin_trending_hashtags") || '' + Redis.current.with do |conn| + @trending_hashtags = conn.get("admin_trending_hashtags") || '' + end end def create - Redis.current.set("admin_trending_hashtags", params[:trending_hashtags]) + Redis.current.with do |conn| + conn.set("admin_trending_hashtags", params[:trending_hashtags]) + end redirect_to admin_trending_hashtags_path end end diff --git a/app/controllers/api/v1/accounts/credentials_controller.rb b/app/controllers/api/v1/accounts/credentials_controller.rb index 29ae8b0a..f3aa4c94 100644 --- a/app/controllers/api/v1/accounts/credentials_controller.rb +++ b/app/controllers/api/v1/accounts/credentials_controller.rb @@ -22,17 +22,19 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController @account = current_account if !@account.user.confirmed? - redisResult = Redis.current.get("account:#{@account.id}:last_email_confirmation_resend") || 0 + Redis.current.with do |conn| + redisResult = conn.get("account:#{@account.id}:last_email_confirmation_resend") || 0 - @lastSentDate = redisResult - if redisResult != 0 - @lastSentDate = Time.at(redisResult.to_i).utc - end - - if @lastSentDate == 0 || (@lastSentDate != 0 && Time.now.utc - @lastSentDate >= 1.hour) - @user = Account.find(@account.id).user || raise(ActiveRecord::RecordNotFound) - Redis.current.set("account:#{@account.id}:last_email_confirmation_resend", Time.now.utc.to_i) - @user.resend_confirmation_instructions + @lastSentDate = redisResult + if redisResult != 0 + @lastSentDate = Time.at(redisResult.to_i).utc + end + + if @lastSentDate == 0 || (@lastSentDate != 0 && Time.now.utc - @lastSentDate >= 1.hour) + @user = Account.find(@account.id).user || raise(ActiveRecord::RecordNotFound) + conn.set("account:#{@account.id}:last_email_confirmation_resend", Time.now.utc.to_i) + @user.resend_confirmation_instructions + end end end diff --git a/app/controllers/api/v1/expenses_controller.rb b/app/controllers/api/v1/expenses_controller.rb index afd4a2d3..5b9eae7b 100644 --- a/app/controllers/api/v1/expenses_controller.rb +++ b/app/controllers/api/v1/expenses_controller.rb @@ -3,7 +3,9 @@ class Api::V1::ExpensesController < EmptyController def show - amount = Redis.current.get("monthly_funding_amount") || 0 + Redis.current.with do |conn| + amount = conn.get("monthly_funding_amount") || 0 + end amount = [amount.to_f, 100].min render json: { "expenses": amount } end diff --git a/app/controllers/api/v1/popular_links_controller.rb b/app/controllers/api/v1/popular_links_controller.rb index 04186e05..ce35a7c0 100644 --- a/app/controllers/api/v1/popular_links_controller.rb +++ b/app/controllers/api/v1/popular_links_controller.rb @@ -28,38 +28,40 @@ class Api::V1::PopularLinksController < Api::BaseController end def get_cards - body = Redis.current.get("popular_links:card_ids") - - if body.nil? || body.empty? - statusIds = Status.where('statuses.created_at > ?', 24.hours.ago) - .joins(:status_stat) - .order('status_stats.favourites_count DESC') - .where('status_stats.favourites_count > 15') - .joins("LEFT JOIN preview_cards_statuses ON statuses.id = preview_cards_statuses.status_id") - .where('preview_cards_statuses.status_id IS NOT NULL') - .limit(100) - .pluck('statuses.id') + Redis.current.with do |conn| + body = conn.get("popular_links:card_ids") + + if body.nil? || body.empty? + statusIds = Status.where('statuses.created_at > ?', 24.hours.ago) + .joins(:status_stat) + .order('status_stats.favourites_count DESC') + .where('status_stats.favourites_count > 15') + .joins("LEFT JOIN preview_cards_statuses ON statuses.id = preview_cards_statuses.status_id") + .where('preview_cards_statuses.status_id IS NOT NULL') + .limit(100) + .pluck('statuses.id') + + cards = PreviewCard.joins("LEFT JOIN preview_cards_statuses ON preview_cards.id = preview_cards_statuses.preview_card_id") + .where('preview_cards_statuses.preview_card_id IS NOT NULL') + .where('preview_cards_statuses.status_id IN (?)', statusIds) + .having('COUNT(preview_cards_statuses.preview_card_id) > 1') + .where('preview_cards.updated_at > ?', 24.hours.ago) + .order('COUNT(preview_cards_statuses.preview_card_id) DESC') + .group('preview_cards.id') + .limit(10) + + card_ids = cards.map(&:id) + + conn.set("popular_links:card_ids", card_ids.join(',')) + conn.expire("popular_links:card_ids", 15.minutes.seconds) + + return cards + else + cards = PreviewCard.where(id: body.split(',')) + return cards + end - cards = PreviewCard.joins("LEFT JOIN preview_cards_statuses ON preview_cards.id = preview_cards_statuses.preview_card_id") - .where('preview_cards_statuses.preview_card_id IS NOT NULL') - .where('preview_cards_statuses.status_id IN (?)', statusIds) - .having('COUNT(preview_cards_statuses.preview_card_id) > 1') - .where('preview_cards.updated_at > ?', 24.hours.ago) - .order('COUNT(preview_cards_statuses.preview_card_id) DESC') - .group('preview_cards.id') - .limit(10) - - card_ids = cards.map(&:id) - - Redis.current.set("popular_links:card_ids", card_ids.join(',')) - Redis.current.expire("popular_links:card_ids", 15.minutes.seconds) - - return cards - else - cards = PreviewCard.where(id: body.split(',')) - return cards end - end end diff --git a/app/controllers/api/v1/trending_hashtags_controller.rb b/app/controllers/api/v1/trending_hashtags_controller.rb index c0a59f5e..aafc44e1 100644 --- a/app/controllers/api/v1/trending_hashtags_controller.rb +++ b/app/controllers/api/v1/trending_hashtags_controller.rb @@ -3,7 +3,9 @@ class Api::V1::TrendingHashtagsController < EmptyController def show - tags = Redis.current.get("admin_trending_hashtags") || "" + Redis.current.with do |conn| + tags = conn.get("admin_trending_hashtags") || "" + end tags = tags.strip.split(", ") render json: { trending_hashtags: tags } end