Skip to content

Commit

Permalink
remove Rabel::ActiveCache
Browse files Browse the repository at this point in the history
ActiveCache was not well designed, and we're going to cache dataset
into redis in the future.
  • Loading branch information
Devin Zhang committed Mar 24, 2015
1 parent f253628 commit 00e5e8c
Show file tree
Hide file tree
Showing 38 changed files with 67 additions and 224 deletions.
1 change: 1 addition & 0 deletions .env.example → .env-example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:
RAILS_ENV=
REDIS_CONFIG_PATH=
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: bundle exec thin start -p $PORT -e $RAILS_ENV
redis: redis-server $REDIS_CONFIG_PATH
4 changes: 4 additions & 0 deletions app/assets/stylesheets/core.css.scss.erb
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,7 @@ ul.nav li {
background: #333;
border-radius: 0;
}

.global-create-btn {
margin-right: 15px;
}
4 changes: 2 additions & 2 deletions app/controllers/nodes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
class NodesController < ApplicationController
def show
@node = Node.find_by_attr_cached!(:key, params[:key])
@node = Node.where(key: params[:key]).first
@title = @node.name

if params[:p].present?
Expand All @@ -15,7 +15,7 @@ def show
@total_pages = (@total_topics * 1.0 / Siteconf.pagination_topics.to_i).ceil
@next_page_num = (@page_num < @total_pages) ? @page_num + 1 : 0
@prev_page_num = (@page_num > 1) ? @page_num - 1 : 0
@topics = @node.cached_assoc_pagination(:topics, @page_num, Siteconf.pagination_topics.to_i, 'updated_at')
@topics = @node.topics.page(@page_num).per(Siteconf.pagination_topics.to_i).order('updated_at DESC')

@canonical_path = "/go/#{params[:key]}"
@canonical_path += "?p=#{@page_num}" if @page_num > 1
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ def show
if current_user && current_user.can_manage_site?
@page = Page.find_by_key!(params[:key])
else
@page = Page.find_by_attr_cached!(:key, params[:key], :published => true)
@page = Page.where(:published => true).find_by!(:key => params[:key])
end

@title = @page.title

if @page.content.size > 100
Expand Down
11 changes: 6 additions & 5 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def index
current_page = 1
end

@topics = Topic.cached_pagination(current_page, per_page, 'updated_at')
@topics = Topic.page(current_page).per(per_page).order('updated_at DESC')

@canonical_path = topics_path
@canonical_path += "?page=#{current_page}" if current_page > 1
Expand All @@ -38,19 +38,19 @@ def index
def show
raise ActiveRecord::RecordNotFound.new if params[:id].to_i.to_s != params[:id]

@topic = Topic.find_cached(params[:id])
@topic = Topic.find(params[:id])
store_location

Topic.increment_counter(:hit, @topic.id)

@title = @topic.title
@node = @topic.cached_assoc_object(:node)
@node = @topic.node

@total_comments = @topic.comments_count
@total_comments = @topic.comments.count
@total_pages = (@total_comments * 1.0 / Siteconf.pagination_comments.to_i).ceil
@current_page = params[:p].nil? ? @total_pages : params[:p].to_i
@per_page = Siteconf.pagination_comments.to_i
@comments = @topic.cached_assoc_pagination(:comments, @current_page, @per_page, 'created_at', Rabel::Model::ORDER_ASC)
@comments = @topic.comments.page(@current_page).per(@per_page).order('created_at ASC')

@new_comment = @topic.comments.new
@total_bookmarks = @topic.bookmarks.count
Expand All @@ -77,6 +77,7 @@ def new

def new_from_home
@topic = Topic.new
@topic.node = Node.find(params[:node_id]) if params[:node_id]
end

def create
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class UsersController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :topics]

def show
@user = User.find_by_attr_cached!(:nickname, params[:nickname])
@user = User.where(:nickname => params[:nickname]).first
store_location

@title = @user.nickname
Expand All @@ -24,10 +24,10 @@ def show
end

def topics
@user = User.find_by_attr_cached!(:nickname, params[:nickname])
@user = User.where(:nickname => params[:nickname]).first

@current_page = params[:page].present? ? params[:page] : 1
@topics = @user.cached_assoc_pagination(:topics, @current_page, 20, 'created_at')
@topics = @user.topics.page(@current_page).per(20).order('created_at DESC')

@title = "#{@user.nickname} 创建的所有主题"
@seo_description = "#{@title} - #{Siteconf.site_name}"
Expand Down
7 changes: 1 addition & 6 deletions app/models/advertisement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ class Advertisement < ActiveRecord::Base
before_validation :set_expire_date

def self.available
num = available_condition.count
return Rabel::Model::EMPTY_DATASET if num == 0
ts = select('updated_at').order('updated_at DESC').first.try(:updated_at)
Rails.cache.fetch("#{self.model_name.collection}/available/#{num}-#{ts}") do
available_condition.order('start_date DESC').all
end
available_condition.order('start_date DESC').all
end

def self.available_condition
Expand Down
2 changes: 2 additions & 0 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Channel < ActiveRecord::Base
end
4 changes: 1 addition & 3 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class Comment < ActiveRecord::Base
include Rabel::ActiveCache

belongs_to :user
belongs_to :commentable, :polymorphic => true, :counter_cache => true

Expand Down Expand Up @@ -60,7 +58,7 @@ def send_notification_to_mentioned_users

def update_last_reply
if commentable.has_attribute?(:last_replied_by) and commentable.has_attribute?(:last_replied_at)
if commentable.comments_count == 0
if commentable.comments.count == 0
commentable.last_replied_by = ''
commentable.last_replied_at = ''
else
Expand Down
3 changes: 0 additions & 3 deletions app/models/following.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# FIXME
# Three indexes were added: :user_id, :followed_user_id and [:user_id, :followed_user_id]
# Need to know if all indexes are needed
class Following < ActiveRecord::Base
belongs_to :follower, :class_name => 'User', :foreign_key => 'user_id'
belongs_to :followed_user, :class_name => 'User'
Expand Down
1 change: 0 additions & 1 deletion app/models/node.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# encoding: utf-8
class Node < ActiveRecord::Base
include Sortable
include Rabel::ActiveCache

has_many :topics
has_many :bookmarks, :as => :bookmarkable, :dependent => :destroy
Expand Down
1 change: 0 additions & 1 deletion app/models/page.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# encoding: utf-8
class Page < ActiveRecord::Base
include Sortable
include Rabel::ActiveCache

acts_as_list

Expand Down
1 change: 0 additions & 1 deletion app/models/plane.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Plane < ActiveRecord::Base
include Rabel::ActiveCache
include Sortable

validates :name, :presence => true
Expand Down
23 changes: 6 additions & 17 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Topic < ActiveRecord::Base
include Notifiable
include Rabel::ActiveCache

DEFAULT_HIT = 0
default_value_for :hit, DEFAULT_HIT
Expand Down Expand Up @@ -40,25 +39,15 @@ def notifiable_path
end

def self.sticky_topics
ts = select('updated_at').with_sticky(true).order('updated_at DESC').first.try(:updated_at)
return Rabel::Model::EMPTY_DATASET unless ts.present?
count = with_sticky(true).count
Rails.cache.fetch("topics/sticky/#{ts}-#{count}") do
with_sticky(true).order('updated_at DESC').all
end
with_sticky(true).order('updated_at DESC').all
end

def self.home_topics(num)
ts = select('updated_at').order('updated_at DESC').first.try(:updated_at)
return Rabel::Model::EMPTY_DATASET unless ts.present?
node_ts = Node.select('updated_at').order('updated_at DESC').first.try(:updated_at)
Rails.cache.fetch("topics/homepage/#{self.count}/#{num}-#{ts}/#{node_ts}") do
excluded_nodes = Node.where(:quiet => true).pluck(:id)
if excluded_nodes.any?
where("node_id NOT in (?)", excluded_nodes).with_sticky(false).latest_involved_topics(num)
else
with_sticky(false).latest_involved_topics(num)
end
excluded_nodes = Node.where(:quiet => true).pluck(:id)
if excluded_nodes.any?
where("node_id NOT in (?)", excluded_nodes).with_sticky(false).latest_involved_topics(num)
else
with_sticky(false).latest_involved_topics(num)
end
end

Expand Down
3 changes: 1 addition & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'carrierwave/orm/activerecord'

class User < ActiveRecord::Base
include Rabel::ActiveCache
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
Expand Down Expand Up @@ -68,7 +67,7 @@ def bookmarked_topics

def recent_followers
follower_ids = self.follower_relationships.order('created_at DESC').limit(10).pluck(:user_id)
follower_ids.map { |uid| User.find_cached(uid) }
follower_ids.map { |uid| User.find(uid) }
end

def follow(user)
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/topics/_table_view.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
%td.auto
= user_profile_link(topic.user)
%td.auto{:align => :right}
= topic.comments_count
= topic.comments.count
%td.auto{:align => :right}
= topic.hit
%td.auto{:align => :right}
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/welcome_admin/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
%th 回复
%tr
%td
%strong= User.cached_count
%strong= User.count
%td
%strong= Topic.cached_count
%strong= Topic.count
%td
%strong= Comment.cached_count
%strong= Comment.count

- if @notifications_to_clear > 0
.row
Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/_comment.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- comment_user = comment.cached_assoc_object(:user)
- comment_user = comment.user
%article
.cell.reply.hoverable{:id => comment.html_id, :class => comment_user.can_manage_site? ? 'admin' : ''}
%table{:cellpadding => 0, :cellspacing => 0, :border => 0, :width => '100%'}
Expand Down
2 changes: 1 addition & 1 deletion app/views/nodes/_bookmark_node.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%tr
%td{:width => '50', :align => :right, :valign => :middle}
.howmany= node.topics_count
.howmany= node.topics.count
%td{:width => :auto, :align => :left}
%h3= link_to node.name, go_path(node.key)

10 changes: 1 addition & 9 deletions app/views/nodes/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.box-header
- if user_signed_in?
.fr
= link_to '创建新话题', '#new_topic', :class => 'btn btn-sm btn-success'
= link_to '在此节点创建新话题', new_from_home_topics_path(:node_id => @node.id), :class => 'btn btn-sm btn-success'
= build_navigation([@node.name], 'bigger')
- if @node.introduction.present?
.sep10
Expand All @@ -30,11 +30,3 @@
.inner
= render 'paginator'

- if user_signed_in?
.box
.box-header
创建新话题
.inner
= render 'topics/form', :node => @node, :topic => @node.topics.new, :comments_closed => false


2 changes: 1 addition & 1 deletion app/views/planes/_plane.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
%td{:align => :right, :width => 100}
= plane.name
%td{:style => 'line-height: 200%; padding-left: 15px;'}
= render plane.cached_assoc_collection(:nodes, Node.default_order_str, 20)
= render plane.nodes.order(Node.default_order_str).limit(20)
2 changes: 1 addition & 1 deletion app/views/topics/_profile_topic.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- topic_node = topic.node
- comments_count = topic.comments_count
- comments_count = topic.comments.count
- last_replied_by = topic.last_replied_by
.cell.topic{:class => topic_user.can_manage_site? ? 'admin' : ''}
%table{:cellpadding => 0, :cellspacing => 0, :border => 0, :width => '100%'}
Expand Down
2 changes: 1 addition & 1 deletion app/views/topics/_table_view.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- i += 1
%tr
%td{:align => :right, :width => 50, :class => "#{class_name} lend"}
- comments_count = topic.comments_count
- comments_count = topic.comments.count
- if comments_count > 0
%strong
%span.green= comments_count
Expand Down
6 changes: 3 additions & 3 deletions app/views/topics/_topic.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- topic_user = topic.cached_assoc_object(:user)
- topic_node = topic.cached_assoc_object(:node)
- comments_count = topic.comments_count
- topic_user = topic.user
- topic_node = topic.node
- comments_count = topic.comments.count
- last_replied_by = topic.last_replied_by
.cell.topic{:class => topic_user.can_manage_site? ? 'admin' : ''}
.avatar.pull-left
Expand Down
2 changes: 1 addition & 1 deletion app/views/topics/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
});

- topic_user = @topic.cached_assoc_object(:user)
- topic_user = @topic.user

.box
%article
Expand Down
2 changes: 1 addition & 1 deletion app/views/welcome/_home_topics.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
.inner
.pull-right= render 'shared/rss'
&nbsp;
- if Topic.cached_count > Siteconf::HOMEPAGE_TOPICS
- if Topic.count > Siteconf::HOMEPAGE_TOPICS
%span.chevron »
= link_to '更多新主题', topics_path + '?page=2', :class => :rabel
2 changes: 1 addition & 1 deletion app/views/welcome/_rightbar_plane.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.cell
%span.gray= plane.name
.sep5
= render plane.cached_assoc_collection(:nodes, Node.default_order_str, 20)
= render plane.nodes.order(Node.default_order_str).limit(20)
2 changes: 1 addition & 1 deletion app/views/welcome/_rightbar_planes.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.box.fix_cell
.box-header
节点导航
= render :partial => 'rightbar_plane', :collection => Plane.cached_all(Plane.default_order_str), :as => :plane
= render :partial => 'rightbar_plane', :collection => Plane.all.order(Plane.default_order_str), :as => :plane

2 changes: 1 addition & 1 deletion app/views/welcome/sitemap.xml.builder
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ xml.urlset :"xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
@topics.each do |topic|
xml.url do
xml.loc(t_url(topic.id))
if topic.comments_count > 0
if topic.comments.count > 0
lastmod = topic.last_comment.updated_at
else
lastmod = topic.updated_at
Expand Down
13 changes: 13 additions & 0 deletions config/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
daemonize no
bind 127.0.0.1
port 6379
timeout 0
databases 16

save 900 1
save 300 10
save 60 10000

dir /usr/local/var/db/redis
appendonly no
appendfsync everysec
5 changes: 0 additions & 5 deletions features/homepage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,3 @@ Feature: Homepage
And I am on the home page
Then I should see Rabel is cool

@javascript
Scenario: search
Given I am on the home page
Then it should display the search form

9 changes: 0 additions & 9 deletions features/nodes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ Feature: Nodes
Then page title should contain 电影
And I should see 电影
And I should see 韩国电影推荐
And it should display a topic creation form

Scenario: create topic on node page
Given a node exists with name: 电影
And I have logged in as devin
When I am on the node page
Then it should display a topic creation form
When I provide topic creation information
Then I should be redirected to the topic page

Scenario: topic pagination
Given a node exists with name: 电影
Expand Down
Loading

0 comments on commit 00e5e8c

Please sign in to comment.