search: cache timed out search counts.

When a search is performed, we cache the post count so we don't have to
calculate it again every time the user switches pages. However, if the
count times out, we didn't cache it before, causing us to do a slow
count on every page load. This usually happens on multi-tag searches
that return a lot of results, `1girl solo` for example.

This changes it so that the count is cached even when it times out. This
will speed up large multi-tag searches.

This also changes it so that the count is cached for a fixed 5 minutes.
Before it was variable based on the size of the count, but this probably
didn't make much difference.
This commit is contained in:
evazion
2021-10-12 00:41:00 -05:00
parent 341be51f95
commit 0b22e873c9
2 changed files with 10 additions and 21 deletions

View File

@@ -902,8 +902,8 @@ class PostQueryBuilder
def fast_count(timeout: 1_000, estimate_count: true, skip_cache: false)
count = nil
count = estimated_count if estimate_count
count = cached_count if count.nil? && !skip_cache
count = exact_count(timeout) if count.nil?
count = cached_count(timeout) if count.nil? && !skip_cache
count = exact_count(timeout) if count.nil? && skip_cache
count
end
@@ -936,22 +936,16 @@ class PostQueryBuilder
ExplainParser.new(build).row_count
end
def cached_count
Cache.get(count_cache_key)
def cached_count(timeout, duration: 5.minutes)
Cache.get(count_cache_key, duration) do
exact_count(timeout)
end
end
def exact_count(timeout)
count = Post.with_timeout(timeout, nil) do
Post.with_timeout(timeout) do
build.count
end
set_cached_count(count) if count.present?
count
end
def set_cached_count(count)
expiry = count.seconds.clamp(3.minutes, 20.hours).to_i
Cache.put(count_cache_key, count, expiry)
end
def count_cache_key