Permalink
Browse files

PERF: introduce full cache for site json when anon

  • Loading branch information...
SamSaffron committed Sep 28, 2015
1 parent 181ab89 commit 1061a9ed06c50083ee6d05c7bac72c86f54433a9
Showing with 43 additions and 1 deletion.
  1. +12 −0 app/models/anon_site_json_cache_observer.rb
  2. +30 −1 app/models/site.rb
  3. +1 −0 lib/site_setting_extension.rb
@@ -0,0 +1,12 @@
class AnonSiteJsonCacheObserver < ActiveRecord::Observer
observe :category, :post_action_type, :user_field, :group

This comment has been minimized.

@eviltrout

eviltrout Sep 28, 2015

Member

what about site settings? They would change the top menu settings and such right?

This comment has been minimized.

@ZogStriP

This comment has been minimized.

@SamSaffron

SamSaffron Oct 2, 2015

Member

@eviltrout see: Site.clear_anon_cache! in site_setting_extension.


def after_destroy(object)
Site.clear_anon_cache!
end

def after_save(object)
Site.clear_anon_cache!
end

end
@@ -85,8 +85,37 @@ def self.json_for(guardian)
}.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
@@ -373,6 +373,7 @@ def set(name, value)
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)

1 comment on commit 1061a9e

@ZogStriP

This comment has been minimized.

Copy link
Member

ZogStriP commented on 1061a9e Sep 28, 2015

👍

Please sign in to comment.