diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-02 15:10:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-02 15:10:35 +0300 |
commit | 4fa04f789e6fed5f0dfeafe718eeb7f56a5086e9 (patch) | |
tree | 5ef2d1d8232d3bd359ec79bf95c9a35ce650ae0b /lib/gitlab/search | |
parent | 4b9ace6c1fead1b44f173eaee0cfaa58f46a258a (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/search')
-rw-r--r-- | lib/gitlab/search/recent_issues.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/gitlab/search/recent_issues.rb b/lib/gitlab/search/recent_issues.rb new file mode 100644 index 00000000000..755ca35bd66 --- /dev/null +++ b/lib/gitlab/search/recent_issues.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module Gitlab + module Search + class RecentIssues + ITEMS_LIMIT = 100 + EXPIRES_AFTER = 7.days + + def initialize(user:, items_limit: ITEMS_LIMIT, expires_after: EXPIRES_AFTER) + @user = user + @items_limit = items_limit + @expires_after = expires_after + end + + def log_view(issue) + return unless recent_items_enabled? + + with_redis do |redis| + redis.zadd(key, Time.now.to_f, issue.id) + redis.expire(key, @expires_after) + + # There is a race condition here where we could end up removing an + # item from 2 places concurrently but this is fine since worst case + # scenario we remove an extra item from the end of the list. + if redis.zcard(key) > @items_limit + redis.zremrangebyrank(key, 0, 0) # Remove least recent + end + end + end + + def search(term) + return Issue.none unless recent_items_enabled? + + ids = with_redis do |redis| + redis.zrevrange(key, 0, @items_limit - 1) + end.map(&:to_i) + + IssuesFinder.new(@user, search: term, in: 'title').execute.reorder(nil).id_in_ordered(ids) # rubocop: disable CodeReuse/ActiveRecord + end + + private + + def with_redis(&blk) + Gitlab::Redis::SharedState.with(&blk) # rubocop: disable CodeReuse/ActiveRecord + end + + def key + "recent_items:#{type.name.downcase}:#{@user.id}" + end + + def type + Issue + end + + def recent_items_enabled? + Feature.enabled?(:recent_items_search, @user) + end + end + end +end |