From 83f80341b3bfdd15d9a1e1afab62ec3b165bd4b0 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Jul 2016 10:20:14 +1000 Subject: [PATCH] FIX: topic id cache should be a multi process cache --- app/models/category.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 444a4ec2b..71e42064a 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -61,8 +61,8 @@ class Category < ActiveRecord::Base has_many :category_tag_groups, dependent: :destroy has_many :tag_groups, through: :category_tag_groups - after_save :clear_topic_ids_cache - after_destroy :clear_topic_ids_cache + after_save :reset_topic_ids_cache + after_destroy :reset_topic_ids_cache scope :latest, -> { order('topic_count DESC') } @@ -86,16 +86,18 @@ class Category < ActiveRecord::Base # we may consider wrapping this in another spot attr_accessor :displayable_topics, :permission, :subcategory_ids, :notification_level, :has_children + @topic_id_cache = DistributedCache.new('category_topic_ids') + def self.topic_ids - @topic_ids ||= Set.new(Category.pluck(:topic_id).compact) + @topic_id_cache['ids'] || reset_topic_ids_cache end - def self.clear_topic_ids_cache - @topic_ids = nil + def self.reset_topic_ids_cache + @topic_id_cache['ids'] = Set.new(Category.pluck(:topic_id).compact) end - def clear_topic_ids_cache - Category.clear_topic_ids_cache + def reset_topic_ids_cache + Category.reset_topic_ids_cache end def self.last_updated_at