PERF: stop querying banner topic on every page hit

This commit is contained in:
Sam 2014-11-14 15:39:17 +11:00
parent 6ca87d281d
commit c7bc692f40
2 changed files with 21 additions and 4 deletions

View File

@ -7,6 +7,7 @@ require_dependency 'rate_limiter'
require_dependency 'crawler_detection'
require_dependency 'json_error'
require_dependency 'letter_avatar'
require_dependency 'distributed_cache'
class ApplicationController < ActionController::Base
include CurrentUser
@ -271,11 +272,21 @@ class ApplicationController < ActionController::Base
MultiJson.dump(data)
end
def self.banner_json_cache
@banner_json_cache ||= DistributedCache.new("banner_json")
end
def banner_json
json = ApplicationController.banner_json_cache["json"]
unless json
topic = Topic.where(archetype: Archetype.banner).limit(1).first
banner = topic.present? ? topic.banner : {}
ApplicationController.banner_json_cache["json"] = json = MultiJson.dump(banner)
end
MultiJson.dump(banner)
json
end
def render_json_error(obj)

View File

@ -172,6 +172,12 @@ class Topic < ActiveRecord::Base
unless skip_callbacks
schedule_auto_close_job
end
banner = "banner".freeze
if archetype_was == banner || archetype == banner
ApplicationController.banner_json_cache.clear
end
end
def initialize_default_values