cache posts/forums on homepage

This commit is contained in:
Skud
2013-08-20 18:59:13 +10:00
parent b913ca7a19
commit 347be0633b
3 changed files with 31 additions and 10 deletions

View File

@@ -1,5 +1,8 @@
class ForumsController < ApplicationController
load_and_authorize_resource
cache_sweeper :forum_sweeper
# GET /forums
# GET /forums.json
def index

View File

@@ -0,0 +1,16 @@
class ForumSweeper < ActionController::Caching::Sweeper
observe Forum
def after_create(forum)
expire_fragment('homepage_forums')
end
def after_update(forum)
expire_fragment('homepage_forums')
end
def after_destroy(forum)
expire_fragment('homepage_forums')
end
end

View File

@@ -1,16 +1,18 @@
%h2 Discussion
- posts = Post.limit(6)
- if posts
=render :partial => "posts/summary", :locals => { :posts => posts, :howmany => 6 }
- cache "recent_posts" do
- posts = Post.limit(6)
- if posts
=render :partial => "posts/summary", :locals => { :posts => posts, :howmany => 6 }
- forums = Forum.all
- if forums
%ul.inline
%li
%strong Forums:
- forums.each do |f|
%li= link_to f.name, f
- cache "homepage_forums" do
- forums = Forum.all
- if forums
%ul.inline
%li
%strong Forums:
- forums.each do |f|
%li= link_to f.name, f
%p.text-right
= link_to "View all posts »", posts_path