Rails fragment caching with personalization -
my site has top navigation menu bar has infrequently changed contents , it's therefore candidate fragment caching. if user signed in, part of menu bar have user's name in it, , therefore personalised , can't cached.
i can either:
fragment cache static part , serve personalised part separately. seems pretty easy.
cache lot, possibly in memcached or cloudfront, keep user's name in session, , use javascript extract user's name session , personalise page @ client end.
what best option go with? preferably based on personal experience of similar situations.
try this:
##in user.rb cache user after_save :invalidate_cache def self.serialize_from_session(key, salt) single_key = key.is_a?(array) ? key.first : key rails.cache.fetch("user:#{single_key}") user.where(:id => single_key).entries.first end end def invalidate_cache rails.cache.delete("user:#{id}") end
credit: rails devise: how (mem)cache devise's database requests user object?
Comments
Post a Comment