feeds/lib/feed_model/cache.rb

48 lines
1.4 KiB
Ruby

module FeedModel
module Cache
require 'active_support/concern'
extend ActiveSupport::Concern
extend self
included do
before_save :do_before_save
end
def recreate_annc_cache(feed)
count = SiteFeedAnnc.where(feed_id: feed.id).count
if count>1
SiteFeedAnnc.where(feed_id: feed.id).limit(count-1).destroy
end
tmp = SiteFeedAnnc.where(feed_id: feed.id).first
if tmp.nil?
tmp = SiteFeedAnnc.new(feed_id: feed.id)
end
tmp.all_contents_for_feed = tmp.cache_annc(true)
tmp.save
if feed.feeds_model && feed.feeds_finish_callback
feeds_model = feed.feeds_model.constantize
feeds_model.send(feed.feeds_finish_callback, "update_all", {"data"=>feed.get_annc(false),"feed_id"=>feed.id,"category_id"=>feed.merge_with_category})
end
end
def do_before_save
unless @skip_callback
if self.class == Category
Thread.new do
SiteFeedAnnc.where(merge_with_category: self.id.to_s).each do |site_feed_annc|
recreate_annc_cache(site_feed_annc)
end
end
elsif self.class == SiteFeed
if self.disabled != true
Thread.new do
recreate_annc_cache(self)
end
else
tmp = SiteFeedAnnc.where(feed_id: self.id).first
if !tmp.nil?
tmp.destroy
end
end
end
end
end
end
end