From 0481ee73f942a3d9b84c6c26c39064168fc7b7c5 Mon Sep 17 00:00:00 2001 From: albert Date: Sun, 6 Jan 2013 16:54:35 -0500 Subject: [PATCH] cache improvements --- app/models/post.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 5938b0de8..17f9db31a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -600,11 +600,13 @@ class Post < ActiveRecord::Base Cache.get(count_cache_key(tags)) end - def set_count_in_cache(tags, count) - if count < 100 - expiry = 0 - else - expiry = (count * 4).minutes + def set_count_in_cache(tags, count, expiry = nil) + if expiry.nil? + if count < 100 + expiry = 0 + else + expiry = (count * 4).minutes + end end Cache.put(count_cache_key(tags), count, expiry) @@ -615,17 +617,21 @@ class Post < ActiveRecord::Base end def fast_count(tags = "") - tags = tags.to_s + tags = tags.to_s.strip count = get_count_from_cache(tags) if count.nil? - count = Post.tag_match(tags).undeleted.count - if count > Danbooru.config.posts_per_page * 10 - set_count_in_cache(tags, count) + if tags.blank? + set_count_in_cache("", 1_000_000, rand(24) * 1.hour) + 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 count 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 rescue SearchError 0