Permalink
Browse files

Add Fragment Cache for article index page

  • Loading branch information...
reyesyang committed Jan 2, 2014
1 parent 3f8bfdc commit 9347bfea9d64f7c9311d858659f1599d0571af99
View
@@ -41,6 +41,7 @@ gem 'wmd-rails'
gem 'redcarpet'
gem "haml"
gem 'rails-i18n', '~>4.0.0'
+gem 'dalli'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.0'
View
@@ -59,6 +59,7 @@ GEM
execjs
coffee-script-source (1.6.3)
commonjs (0.2.7)
+ dalli (2.6.4)
database_cleaner (1.2.0)
diff-lcs (1.2.5)
docile (1.1.1)
@@ -229,6 +230,7 @@ DEPENDENCIES
capistrano-rvm (~> 0.1.0)
capybara
coffee-rails (~> 4.0.0)
+ dalli
database_cleaner
factory_girl_rails
fakeweb
@@ -9,4 +9,5 @@
*= require global
*= require 'wmd/wmd'
*= require 'wmd-editor'
+ *= require articles
*/
@@ -0,0 +1,23 @@
+.article {
+ .tags {
+ display: inline-block;
+ float: left;
+
+ span {
+ float: left;
+ }
+
+ ul {
+ display: inline-block;
+ margin: 0;
+
+ .tag{
+ float: left;
+
+ .count {
+ display: none;
+ }
+ }
+ }
+ }
+}
@@ -18,4 +18,9 @@ def markdown(text)
def join_tags(article)
article.tags.map { |tag| "<a href='/articles/tag/#{tag.id}'>#{tag.name}</a>" }.join("; ")
end
+
+ def cache_key_for_tags
+ last_updated_at = Tag.maximum(:updated_at).try(:utc).try(:to_s, :number)
+ "tags-#{last_updated_at}"
+ end
end
View
@@ -9,11 +9,7 @@ def to_param
name
end
- def self.list(user)
- if user && user.admin?
- Tag.all.load
- else
- Tag.where("name != 'draft'").load
- end
+ def draft?
+ name == 'draft'
end
end
@@ -1,18 +1,16 @@
-%article.article
- %header
- %h3.article_title
- = link_to article.title, article_url(article)
- %span.created_at<
- = t 'post_at'
- = article.created_at.in_time_zone(+8.hours).strftime('%D %T')
- - if article.tags.any?
- %span.tags
- = t 'tags'
- - article.tags.each do |tag|
- = link_to tagging_path(tag) do
- %span.label #{tag.name}
- -if logged_in? && current_user.admin?
- %span.operation
+- cache article do
+ %article.article.clearfix
+ %header
+ %h3.article_title
+ = link_to article.title, article_url(article)
+ %span.created_at<
+ = t 'post_at'
+ = article.created_at.in_time_zone(+8.hours).strftime('%D %T')
+ - if article.tags.any?
+ .tags
+ %span= t 'tags'
+ %ul.unstyled
+ = render partial: 'tags/tag', collection: article.tags
+ %span.operation.admin
= link_to t('edit'), edit_article_path(article)
= link_to t('destroy'), article, data: { confirm: 'Are you sure?' }, :method => :delete
- .clear
@@ -13,13 +13,6 @@
%section#aside_tags
- - @tags = Tag.ordered.list(current_user)
- - if @tags.any?
+ - cache cache_key_for_tags do
%ul.unstyled
- - @tags.each do |tag|
- %li<
- = link_to tagging_path(tag), :class=>'aside_tag' do
- <span class='label'>#{tag.name}</span>
- %span x #{tag.articles_count}
- - else
- No more tags now!
+ = render partial: 'tags/tag', collection: Tag.order(:name).all
@@ -1,13 +0,0 @@
-<%= form.fields_for :tags do |tag_form| %>
- <div class"field">
- <%= tag_form.label :name, 'Tag' %></br>
- <%= tag_form.text_field :name %>
- </div>
-
- <% unless tag_form.object.nil? || tag_form.object.new_record? %>
- <div class="field">
- <%= tag_form.label :_destroy, 'Remove:' %>
- <%= tag_form.check_box :_destroy %>
- </div>
- <% end %>
-<% end %>
@@ -0,0 +1,6 @@
+- cache tag do
+ %li{class: tag.draft? ? "tag admin" : "tag"}
+ = link_to tagging_path(tag), :class=>'aside_tag' do
+ %span{class: 'label'}= tag.name
+ %span.count
+ &times; #{tag.articles_count}
View
@@ -45,6 +45,7 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ config.i18n.enforce_available_locales = true
config.i18n.default_locale = :"zh-CN"
# Configure sensitive parameters which will be filtered from the log file.
@@ -53,5 +54,7 @@ class Application < Rails::Application
# Enable the asset pipeline
config.assets.enabled = true
config.assets.precompile += %w(wmd-buttons.png persona.js *.png *.jpg *.jpeg *.gif)
+
+ config.cache_store = :mem_cache_store
end
end
@@ -12,7 +12,7 @@
# Show full error reports and disable caching
config.consider_all_requests_local = true
- config.action_controller.perform_caching = false
+ config.action_controller.perform_caching = true
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false

0 comments on commit 9347bfe

Please sign in to comment.