cache improvements

This commit is contained in:
albert
2013-01-06 16:54:35 -05:00
parent ac238d9f89
commit 0481ee73f9

View File

@@ -600,11 +600,13 @@ class Post < ActiveRecord::Base
Cache.get(count_cache_key(tags)) Cache.get(count_cache_key(tags))
end end
def set_count_in_cache(tags, count) def set_count_in_cache(tags, count, expiry = nil)
if count < 100 if expiry.nil?
expiry = 0 if count < 100
else expiry = 0
expiry = (count * 4).minutes else
expiry = (count * 4).minutes
end
end end
Cache.put(count_cache_key(tags), count, expiry) Cache.put(count_cache_key(tags), count, expiry)
@@ -615,17 +617,21 @@ class Post < ActiveRecord::Base
end end
def fast_count(tags = "") def fast_count(tags = "")
tags = tags.to_s tags = tags.to_s.strip
count = get_count_from_cache(tags) count = get_count_from_cache(tags)
if count.nil? if count.nil?
count = Post.tag_match(tags).undeleted.count if tags.blank?
if count > Danbooru.config.posts_per_page * 10 set_count_in_cache("", 1_000_000, rand(24) * 1.hour)
set_count_in_cache(tags, count) else
count = Post.tag_match(tags).undeleted.count
if count > Danbooru.config.posts_per_page * 10
set_count_in_cache(tags, count)
end
end end
end end
count count
rescue ActiveRecord::StatementInvalid rescue ActiveRecord::StatementInvalid
set_count_in_cache(tags, rand(24) * 1.hour) set_count_in_cache(tags, 1_000_000, rand(24) * 1.hour)
1_000_000 1_000_000
rescue SearchError rescue SearchError
0 0