m4tm4t
m4tm4t

Reputation: 2381

Rails caching for a forum

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 ?

Upvotes: 1

Views: 139

Answers (2)

m4tm4t
m4tm4t

Reputation: 2381

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

Upvotes: 2

DavidRH
DavidRH

Reputation: 819

Consider using fragment caching. You may cache the whole post or just part of it. In our social app we used it to cache wall posts - the part where info about user is displayed and the post body. The part with buttons like "delete", "edit", "report" was generated on every load depending on the type of the user, e.g. the owner could delete or edit, but non-owner only report.

Fragment caching is performed in view.

Upvotes: 0

Related Questions