2
0
mirror of https://github.com/moebooru/moebooru synced 2025-08-22 01:47:48 +00:00
moebooru/app/models/post/count_methods.rb
petopeto 30ff4fccd3 --HG--
branch : moe
extra : convert_revision : svn%3A2d28d66d-8d94-df11-8c86-00306ef368cb/trunk/moe%405
2010-04-20 23:05:11 +00:00

50 lines
1.5 KiB
Ruby

module PostCountMethods
module ClassMethods
def fast_count(tags = nil)
cache_version = Cache.get("$cache_version").to_i
key = "post-count/v=#{cache_version}/#{tags}"
# memcached protocol is dumb so we need to escape spaces
key = key.gsub(/-/, "--").gsub(/ /, "-_")
count = Cache.get(key) {
Post.count_by_sql(Post.generate_sql(tags, :count => true))
}.to_i
return count
# This is just too brittle, and hard to make work with other features that may
# hide posts from the index.
# if tags.blank?
# return select_value_sql("SELECT row_count FROM table_data WHERE name = 'posts'").to_i
# else
# c = select_value_sql("SELECT post_count FROM tags WHERE name = ?", tags).to_i
# if c == 0
# return Post.count_by_sql(Post.generate_sql(tags, :count => true))
# else
# return c
# end
# end
end
def recalculate_row_count
execute_sql("UPDATE table_data SET row_count = (SELECT COUNT(*) FROM posts WHERE parent_id IS NULL AND status <> 'deleted') WHERE name = 'posts'")
end
end
def self.included(m)
m.extend(ClassMethods)
m.after_create :increment_count
m.after_delete :decrement_count
m.after_undelete :increment_count
end
def increment_count
execute_sql("UPDATE table_data SET row_count = row_count + 1 WHERE name = 'posts'")
end
def decrement_count
execute_sql("UPDATE table_data SET row_count = row_count - 1 WHERE name = 'posts'")
end
end