FIX: topic id cache should be a multi process cache

This commit is contained in:
Sam 2016-07-28 10:20:14 +10:00
parent ab68e0c9db
commit 83f80341b3

View file

@ -61,8 +61,8 @@ class Category < ActiveRecord::Base
has_many :category_tag_groups, dependent: :destroy has_many :category_tag_groups, dependent: :destroy
has_many :tag_groups, through: :category_tag_groups has_many :tag_groups, through: :category_tag_groups
after_save :clear_topic_ids_cache after_save :reset_topic_ids_cache
after_destroy :clear_topic_ids_cache after_destroy :reset_topic_ids_cache
scope :latest, -> { order('topic_count DESC') } scope :latest, -> { order('topic_count DESC') }
@ -86,16 +86,18 @@ class Category < ActiveRecord::Base
# we may consider wrapping this in another spot # we may consider wrapping this in another spot
attr_accessor :displayable_topics, :permission, :subcategory_ids, :notification_level, :has_children attr_accessor :displayable_topics, :permission, :subcategory_ids, :notification_level, :has_children
@topic_id_cache = DistributedCache.new('category_topic_ids')
def self.topic_ids def self.topic_ids
@topic_ids ||= Set.new(Category.pluck(:topic_id).compact) @topic_id_cache['ids'] || reset_topic_ids_cache
end end
def self.clear_topic_ids_cache def self.reset_topic_ids_cache
@topic_ids = nil @topic_id_cache['ids'] = Set.new(Category.pluck(:topic_id).compact)
end end
def clear_topic_ids_cache def reset_topic_ids_cache
Category.clear_topic_ids_cache Category.reset_topic_ids_cache
end end
def self.last_updated_at def self.last_updated_at