PERF: introduce full cache for site json when anon

This commit is contained in:
Sam 2015-09-28 16:44:03 +10:00
parent 181ab89485
commit 1061a9ed06
3 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,12 @@
class AnonSiteJsonCacheObserver < ActiveRecord::Observer
observe :category, :post_action_type, :user_field, :group
def after_destroy(object)
Site.clear_anon_cache!
end
def after_save(object)
Site.clear_anon_cache!
end
end

View File

@ -85,8 +85,37 @@ class Site
}.to_json
end
seq = nil
if guardian.anonymous?
seq = MessageBus.last_id('/site_json')
cached_json, cached_seq, cached_version = $redis.mget('site_json', 'site_json_seq', 'site_json_version')
if cached_json && seq == cached_seq.to_i && Discourse.git_version == cached_version
return cached_json
end
end
site = Site.new(guardian)
MultiJson.dump(SiteSerializer.new(site, root: false, scope: guardian))
json = MultiJson.dump(SiteSerializer.new(site, root: false, scope: guardian))
if guardian.anonymous?
$redis.multi do
$redis.setex 'site_json', 1800, json
$redis.set 'site_json_seq', seq
$redis.set 'site_json_version', Discourse.git_version
end
end
json
end
def self.clear_anon_cache!
# publishing forces the sequence up
# the cache is validated based on the sequence
MessageBus.publish('/site_json','')
end
end

View File

@ -373,6 +373,7 @@ module SiteSettingExtension
def clear_cache!
SiteText.text_for_cache.clear
Rails.cache.delete(SiteSettingExtension.client_settings_cache_key)
Site.clear_anon_cache!
end
def diff_hash(new_hash, old)