From 730b9ca196aa941bfb49a8982dfa6995175761ef Mon Sep 17 00:00:00 2001 From: chiu Date: Wed, 6 May 2020 19:30:09 +0800 Subject: [PATCH] accelerate the speed of generate data for widget by cache --- app/controllers/announcements_controller.rb | 30 ++++++++++++++------- app/models/bulletin.rb | 1 + lib/bulletin_model/cache.rb | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index f71e946..90db09e 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -116,8 +116,9 @@ class AnnouncementsController < ApplicationController subpart = OrbitHelper.get_current_widget anns_cache = AnnsCache.where(parent_id: subpart.id.to_s,locale: I18n.locale.to_s) widget_data_count = OrbitHelper.widget_data_count - set_image_version_for_widget - if !(defined? SiteFeed).nil? || anns_cache.count != 1 || is_random + set_image_version_for_widget() + devide_flag = (!(defined? SiteFeed).nil?) + if anns_cache.count != 1 || is_random page = Page.where(:module => "announcement").first rescue nil Bulletin.remove_expired_status uid = OrbitHelper.params[:uid] rescue "" @@ -129,17 +130,26 @@ class AnnouncementsController < ApplicationController if anns_cache.count > 1 anns_cache.destroy end - AnnsCache.create(parent_id: subpart.id.to_s,locale: I18n.locale.to_s,filter_result: sorted_anns.to_a.to_yaml) + if devide_flag + now_anns = sorted_anns.to_a + top_anns = now_anns.select{|v| v.is_top}.map{|v| data_to_human_type(v)} + not_top_anns = now_anns.select{|v| !v.is_top}.map{|v| data_to_human_type(v)} + AnnsCache.create(parent_id: subpart.id.to_s,locale: I18n.locale.to_s,filter_result: {top: top_anns,not_top: not_top_anns}) + else + anns = sorted_anns.map{|v| data_to_human_type(v)} + AnnsCache.create(parent_id: subpart.id.to_s,locale: I18n.locale.to_s,filter_result: anns) + end else - sorted_anns = sorted_anns.sample(widget_data_count) + anns = sorted_anns.sample(widget_data_count).map{|v| data_to_human_type(v)} end + elsif devide_flag + now_anns = anns_cache.first.filter_result + top_anns = now_anns[:top] + not_top_anns = now_anns[:not_top] else - sorted_anns = YAML.load(anns_cache.first.filter_result) + anns = anns_cache.first.filter_result end - if (defined? SiteFeed).nil? || is_random - anns = sorted_anns.map{|v| data_to_human_type(v)} - else - top_anns = sorted_anns.select{|v| v.is_top}.map{|v| data_to_human_type(v)} + if devide_flag rest_count = widget_data_count - top_anns.count if rest_count <= 0 anns = top_anns @@ -147,7 +157,7 @@ class AnnouncementsController < ApplicationController feeds_anns = get_feed_announcements("widget") top_anns = top_anns + feeds_anns.select{|v| v['is_top']} top_anns = top_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} - rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + sorted_anns.select{|v| !v.is_top}.take(rest_count).map{|v| data_to_human_type(v)} + rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + not_top_anns.take(rest_count) rest_anns = rest_all_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}.take(rest_count) anns = (top_anns + rest_anns).take(widget_data_count) end diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 56023e0..c70e628 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -11,6 +11,7 @@ class Bulletin require 'bulletin_model/cache' include BulletinModel::Cache SubPart.class_eval { include BulletinModel::Cache } + Page.class_eval { include BulletinModel::Cache } before_destroy do AnnsCache.all.destroy end diff --git a/lib/bulletin_model/cache.rb b/lib/bulletin_model/cache.rb index 3928cd3..a8bb061 100644 --- a/lib/bulletin_model/cache.rb +++ b/lib/bulletin_model/cache.rb @@ -8,7 +8,7 @@ module BulletinModel def do_before_save if self.class == SubPart AnnsCache.where(parent_id:self.id).destroy - elsif self.class == Bulletin + elsif self.class == Bulletin || (self.class == Page && self.module == "announcement") AnnsCache.all.destroy end end