Skip to content
Browse files

cache post preview

  • Loading branch information...
1 parent 0819194 commit de91a99453ac8719f649b993821ce36e5760a711 @releu releu committed Apr 29, 2012
View
2 app/controllers/comments_controller.rb
@@ -1,6 +1,4 @@
class CommentsController < ApplicationController
- cache_sweeper :post_sweeper, :only => [:create]
-
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
View
3 app/controllers/posts_controller.rb
@@ -1,5 +1,4 @@
class PostsController < ApplicationController
- cache_sweeper :post_sweeper, :only => [:like, :memorize, :forgot]
before_filter :authenticate!, :except => [:show, :index]
before_filter :handle_index, :only => :index
prepend_before_filter :handle_unlogged, :only => [:flow, :all],
@@ -12,7 +11,7 @@ def index
alias all index
def flow
- @posts = current_user.intrested_posts.page(params[:page])
+ @posts = current_user.intrested_posts.includes(:user).page(params[:page])
end
def show
View
23 app/models/post.rb
@@ -20,6 +20,8 @@ class Post < ActiveRecord::Base
attr_accessible :title, :content, :question, :status
after_create :tweet
+ after_save :cache
+ after_destroy :clear_cache
def link_name
ln = title.blank? ? preview : title
@@ -43,6 +45,15 @@ def status?
status.present?
end
+ def formatted_preview(reload = false)
+ @formatted_preview = nil if reload
+ @formatted_preview ||= Markdown.markdown begin
+ raw = Replaceable.new(preview)
+ raw.replace_gists!.replace_tags!.replace_usernames!
+ raw.to_s
+ end
+ end
+
protected
def cuts_count
@@ -67,4 +78,16 @@ def tweet
user.twitter_client.update(status)
end
end
+
+ def cache
+ $redis.set cache_key(:preview), formatted_preview
+ end
+
+ def clear_cache
+ $redis.del cache_key
+ end
+
+ def cache_key(*paths)
+ [:posts, id, paths].flatten.compact.join(':')
+ end
end
View
10 app/presenters/posts/show_presenter.rb
@@ -12,16 +12,8 @@ def initialize(post)
@post = post
end
- def cache_key
- "post:#{post.id}"
- end
-
def preview
- @preview ||= (Markdown.markdown begin
- raw = Replaceable.new(post.preview)
- raw.replace_gists!.replace_tags!.replace_usernames!
- raw.to_s
- end)
+ post.formatted_preview
end
def body
View
19 app/sweepers/post_sweeper.rb
@@ -1,19 +0,0 @@
-class PostSweeper < ActionController::Caching::Sweeper
- observe Post
-
- def after_update(post)
- clear_cache post
- end
-
- def after_destroy(post)
- clear_cache post
- end
-
-protected
-
- def clear_cache(post)
- presenter = Posts::ShowPresenter.new(post)
- expire_fragment presenter.cache_key
- end
-
-end
View
2 config/application.rb
@@ -25,7 +25,7 @@ class Application < Rails::Application
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
- config.active_record.observers = :post_sweeper
+ # config.active_record.observers = :post_sweeper
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
View
5 lib/tasks/fixes.rake
@@ -0,0 +1,5 @@
+namespace :fixes do
+ task setup_cache_for_current_posts: :environment do
+ Post.all.each(&:save)
+ end
+end
View
11 spec/models/post_spec.rb
@@ -1,9 +1,14 @@
require 'spec_helper'
describe Post do
- describe "#creating posts that includes gists" do
- context 'check that gist tag is parsed properly' do
-
+ let(:post) { create(:post) }
+ subject { post }
+
+ describe 'cache' do
+ let!(:post) { create(:post) }
+
+ it 'should cache preview' do
+ $redis.get("posts:#{post.id}:preview").should == post.formatted_preview
end
end

0 comments on commit de91a99

Please sign in to comment.
Something went wrong with that request. Please try again.