Redis connection pooling updates
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user