Rewrite the implementation of related tags to be simpler, faster, and more accurate: * The related tags are now calculated by taking a random sample of 1000 posts, finding the top 250 most frequent tags among those posts, then ordering those tags by cosine similarity. * Related tags can generally be calculated in 50-300ms at these sample sizes. Very high sample sizes (25000+ posts) are still relatively fast (1-3 seconds), but generally they don't improve accuracy much. * Related tags are now cached in redis rather than in the tags table. The related_tags column in the tags table is no longer used. * Only the related tags in the search taglist are cached. The related tags returned by the 'Related tags' button are not cached. * The cache lifetime is a fixed 4 hours. * The 'Related tags' button now works with metatags. * The /related_tag page now works with metatags and multitag searches. Fixes #4134, #4146.
43 lines
1.0 KiB
Ruby
43 lines
1.0 KiB
Ruby
class Cache
|
|
def self.get_multi(keys, prefix)
|
|
sanitized_key_to_key_hash = keys.map do |key|
|
|
["#{prefix}:#{Cache.hash(key)}", key]
|
|
end.to_h
|
|
|
|
sanitized_keys = sanitized_key_to_key_hash.keys
|
|
sanitized_key_to_value_hash = Rails.cache.fetch_multi(*sanitized_keys) do |sanitized_key|
|
|
key = sanitized_key_to_key_hash[sanitized_key]
|
|
yield key
|
|
end
|
|
|
|
keys_to_values_hash = sanitized_key_to_value_hash.transform_keys(&sanitized_key_to_key_hash)
|
|
keys_to_values_hash
|
|
end
|
|
|
|
def self.get(key, expiry_in_seconds = nil, **options, &block)
|
|
Rails.cache.fetch(key, expires_in: expiry_in_seconds, **options, &block)
|
|
end
|
|
|
|
def self.put(key, value, expiry_in_seconds = nil)
|
|
Rails.cache.write(key, value, expires_in: expiry_in_seconds)
|
|
value
|
|
end
|
|
|
|
def self.delete(key)
|
|
Rails.cache.delete(key)
|
|
nil
|
|
end
|
|
|
|
def self.clear
|
|
Rails.cache.clear
|
|
end
|
|
|
|
def self.sanitize(key)
|
|
key.gsub(/\W/) {|x| "%#{x.ord}"}.slice(0, 230)
|
|
end
|
|
|
|
def self.hash(string)
|
|
CityHash.hash64(string).to_s(36)
|
|
end
|
|
end
|