Change hashtag trends to be stored in the database instead of redis (#32837)
Co-authored-by: David Roetzel <david@roetzel.de>
This commit is contained in:
parent
b9b26490e7
commit
48ea7552dd
18 changed files with 195 additions and 157 deletions
|
@ -34,19 +34,7 @@ class Trends::Base
|
|||
end
|
||||
|
||||
def query
|
||||
Trends::Query.new(key_prefix, klass)
|
||||
end
|
||||
|
||||
def score(id, locale: nil)
|
||||
redis.zscore([key_prefix, 'all', locale].compact.join(':'), id) || 0
|
||||
end
|
||||
|
||||
def rank(id, locale: nil)
|
||||
redis.zrevrank([key_prefix, 'allowed', locale].compact.join(':'), id)
|
||||
end
|
||||
|
||||
def currently_trending_ids(allowed, limit)
|
||||
redis.zrevrange(allowed ? "#{key_prefix}:allowed" : "#{key_prefix}:all", 0, limit.positive? ? limit - 1 : limit).map(&:to_i)
|
||||
Trends::Query.new(klass)
|
||||
end
|
||||
|
||||
protected
|
||||
|
@ -64,42 +52,9 @@ class Trends::Base
|
|||
redis.expire(used_key(at_time), 1.day.seconds)
|
||||
end
|
||||
|
||||
def score_at_rank(rank)
|
||||
redis.zrevrange("#{key_prefix}:allowed", 0, rank, with_scores: true).last&.last || 0
|
||||
end
|
||||
|
||||
def replace_items(suffix, items)
|
||||
tmp_prefix = "#{key_prefix}:tmp:#{SecureRandom.alphanumeric(6)}#{suffix}"
|
||||
allowed_items = filter_for_allowed_items(items)
|
||||
|
||||
redis.pipelined do |pipeline|
|
||||
items.each { |item| pipeline.zadd("#{tmp_prefix}:all", item[:score], item[:item].id) }
|
||||
allowed_items.each { |item| pipeline.zadd("#{tmp_prefix}:allowed", item[:score], item[:item].id) }
|
||||
|
||||
rename_set(pipeline, "#{tmp_prefix}:all", "#{key_prefix}:all#{suffix}", items)
|
||||
rename_set(pipeline, "#{tmp_prefix}:allowed", "#{key_prefix}:allowed#{suffix}", allowed_items)
|
||||
end
|
||||
end
|
||||
|
||||
def filter_for_allowed_items(items)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def used_key(at_time)
|
||||
"#{key_prefix}:used:#{at_time.beginning_of_day.to_i}"
|
||||
end
|
||||
|
||||
def rename_set(pipeline, from_key, to_key, set_items)
|
||||
if set_items.empty?
|
||||
pipeline.del(to_key)
|
||||
else
|
||||
pipeline.rename(from_key, to_key)
|
||||
end
|
||||
end
|
||||
|
||||
def skip_review?
|
||||
Setting.trendable_by_default
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue