I’m trying to implement a cache in my forums, and the hard part is to kept roles & groups.
So a solution that seems to be good is to use action caching to run some before_filter and define the cache_path in a proc.
class Forums::TopicsController < Forums::BaseController
before_filter :authenticate_user!, except: :show
before_filter :load_resources
cache_sweeper :topic_sweeper
caches_action :show, cache_path: proc {
if user_signed_in?
if @topic.user == current_user || current_user.has_role?(:moderator) || current_user.has_role?(:superadmin)
"author_forum_topic_#{@topic.id}"
end
else
forum_topic_path(@forum, @topic)
end
}
def show
@post = Fo::Post.new
end
def create
# ...
end
private
def load_resources
@forum = Fo::Forum.find(params[:forum_id])
@category = @forum.category
@topic = @forum.topics.find(params[:id]) if !%w(create new).include?(action_name)
if %w(show).include?(action_name)
authorize! :read, @topic
@topic.register_view_by(current_user)
end
end
end
This controller look simple, but categories/forums listing are “groups” accessible, so here I can build a sum of groups ids in the cache_path
what do you think about these caching practice ?
After trying many ways for caching my forum, I choosen using Cells https://github.com/apotonick/cells
Full page caching is too hard when adding more functionality
Cells are better way to make fragment caching and testing