From 5e7873285fc961702d9bb2764f67ef0f3e9bb7d4 Mon Sep 17 00:00:00 2001 From: chiu Date: Wed, 22 Jul 2020 11:11:58 +0800 Subject: [PATCH] fix feed cache error --- .../admin/announcements_controller.rb | 17 +++++++++++++++-- .../announcement_feeds_controller.rb | 4 ++-- app/models/bulletin_feed.rb | 4 +++- lib/bulletin_model/cache.rb | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 8ac7404..46f5623 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -154,6 +154,7 @@ class Admin::AnnouncementsController < OrbitAdminController def createfeed announcement_feed = BulletinFeed.new(feed_params) announcement_feed.save + create_feed_cache(nil,announcement_feed) feeds = BulletinFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end @@ -162,6 +163,7 @@ class Admin::AnnouncementsController < OrbitAdminController ann_feed = BulletinFeed.find(params[:id]) ann_feed.update_attributes(feed_params) ann_feed.save + create_feed_cache(nil,ann_feed) feeds = BulletinFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end @@ -481,8 +483,19 @@ class Admin::AnnouncementsController < OrbitAdminController params.require(:announcement_setting).permit! end - def create_feed_cache(bulletin) - BulletinFeed.where(:tag_ids.in => Array(bulletin.tag_ids).collect{|v| v.to_s}).each do |bulletin_feed| + def create_feed_cache(bulletin=nil,bulletin_feed=nil) + if !bulletin.nil? + BulletinFeed.where(:tag_ids.in => Array(bulletin.tag_ids).collect{|v| v.to_s}).each do |bulletin_feed| + uid = bulletin_feed.uid + uri = URI(request.protocol + request.host_with_port + "/xhr/announcements/feed/#{uid}.json") + Thread.new do + res_net = Net::HTTP.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 60,read_timeout: 60) do |http| + req = Net::HTTP::Get.new(uri) + http.request(req) + end + end + end + elsif !bulletin_feed.nil? uid = bulletin_feed.uid uri = URI(request.protocol + request.host_with_port + "/xhr/announcements/feed/#{uid}.json") Thread.new do diff --git a/app/controllers/announcement_feeds_controller.rb b/app/controllers/announcement_feeds_controller.rb index 54fde71..d636008 100644 --- a/app/controllers/announcement_feeds_controller.rb +++ b/app/controllers/announcement_feeds_controller.rb @@ -31,8 +31,8 @@ class AnnouncementFeedsController < ApplicationController end respond_to do |format| format.html {redirect_to "/xhr/announcements/rssfeed/#{@bf.uid}.rss"} - format.rss - end + format.rss + end end def feeds diff --git a/app/models/bulletin_feed.rb b/app/models/bulletin_feed.rb index f577728..892fa88 100644 --- a/app/models/bulletin_feed.rb +++ b/app/models/bulletin_feed.rb @@ -5,5 +5,7 @@ class BulletinFeed field :title, as: :slug_title, type: String, localize: true field :tag_ids, type: Array, default: [] - + before_save do + BulletinFeedCache.where(uid: self.uid).destroy + end end \ No newline at end of file diff --git a/lib/bulletin_model/cache.rb b/lib/bulletin_model/cache.rb index cbb38d7..d595629 100644 --- a/lib/bulletin_model/cache.rb +++ b/lib/bulletin_model/cache.rb @@ -10,7 +10,7 @@ module BulletinModel AnnsCache.where(parent_id:self.id).destroy elsif self.class == Bulletin || (self.class == Page && self.module == "announcement") if self.class == Bulletin - BulletinFeedCache.where(:tag_ids.in => Array(self.tag_ids).collect{|v| v.to_s}).destroy + BulletinFeedCache.where(:uid.in => BulletinFeed.where(:tag_ids.in => Array(self.tag_ids).collect{|v| v.to_s}).pluck(:uid)).destroy end AnnsCache.all.destroy end