accelerate the speed of generate data for widget by cache

This commit is contained in:
chiu 2020-05-06 19:30:09 +08:00
parent 85990441de
commit 730b9ca196
3 changed files with 22 additions and 11 deletions

View File

@ -116,8 +116,9 @@ class AnnouncementsController < ApplicationController
subpart = OrbitHelper.get_current_widget subpart = OrbitHelper.get_current_widget
anns_cache = AnnsCache.where(parent_id: subpart.id.to_s,locale: I18n.locale.to_s) anns_cache = AnnsCache.where(parent_id: subpart.id.to_s,locale: I18n.locale.to_s)
widget_data_count = OrbitHelper.widget_data_count widget_data_count = OrbitHelper.widget_data_count
set_image_version_for_widget set_image_version_for_widget()
if !(defined? SiteFeed).nil? || anns_cache.count != 1 || is_random devide_flag = (!(defined? SiteFeed).nil?)
if anns_cache.count != 1 || is_random
page = Page.where(:module => "announcement").first rescue nil page = Page.where(:module => "announcement").first rescue nil
Bulletin.remove_expired_status Bulletin.remove_expired_status
uid = OrbitHelper.params[:uid] rescue "" uid = OrbitHelper.params[:uid] rescue ""
@ -129,17 +130,26 @@ class AnnouncementsController < ApplicationController
if anns_cache.count > 1 if anns_cache.count > 1
anns_cache.destroy anns_cache.destroy
end 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 else
sorted_anns = sorted_anns.sample(widget_data_count) anns = sorted_anns.sample(widget_data_count).map{|v| data_to_human_type(v)}
end end
elsif devide_flag
now_anns = anns_cache.first.filter_result
top_anns = now_anns[:top]
not_top_anns = now_anns[:not_top]
else else
sorted_anns = YAML.load(anns_cache.first.filter_result) anns = anns_cache.first.filter_result
end end
if (defined? SiteFeed).nil? || is_random if devide_flag
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)}
rest_count = widget_data_count - top_anns.count rest_count = widget_data_count - top_anns.count
if rest_count <= 0 if rest_count <= 0
anns = top_anns anns = top_anns
@ -147,7 +157,7 @@ class AnnouncementsController < ApplicationController
feeds_anns = get_feed_announcements("widget") feeds_anns = get_feed_announcements("widget")
top_anns = top_anns + feeds_anns.select{|v| v['is_top']} top_anns = top_anns + feeds_anns.select{|v| v['is_top']}
top_anns = top_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} 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) rest_anns = rest_all_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}.take(rest_count)
anns = (top_anns + rest_anns).take(widget_data_count) anns = (top_anns + rest_anns).take(widget_data_count)
end end

View File

@ -11,6 +11,7 @@ class Bulletin
require 'bulletin_model/cache' require 'bulletin_model/cache'
include BulletinModel::Cache include BulletinModel::Cache
SubPart.class_eval { include BulletinModel::Cache } SubPart.class_eval { include BulletinModel::Cache }
Page.class_eval { include BulletinModel::Cache }
before_destroy do before_destroy do
AnnsCache.all.destroy AnnsCache.all.destroy
end end

View File

@ -8,7 +8,7 @@ module BulletinModel
def do_before_save def do_before_save
if self.class == SubPart if self.class == SubPart
AnnsCache.where(parent_id:self.id).destroy AnnsCache.where(parent_id:self.id).destroy
elsif self.class == Bulletin elsif self.class == Bulletin || (self.class == Page && self.module == "announcement")
AnnsCache.all.destroy AnnsCache.all.destroy
end end
end end