Class: Users::UpdateTodoCountCacheService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/users/update_todo_count_cache_service.rb

Constant Summary collapse

QUERY_BATCH_SIZE =
10

Constants inherited from BaseService

BaseService::UnauthorizedError

Instance Attribute Summary collapse

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

#initialize(user_ids) ⇒ UpdateTodoCountCacheService

user_ids - An array of User IDs



10
11
12
# File 'app/services/users/update_todo_count_cache_service.rb', line 10

def initialize(user_ids)
  @user_ids = user_ids
end

Instance Attribute Details

#user_idsObject (readonly)

Returns the value of attribute user_ids.



7
8
9
# File 'app/services/users/update_todo_count_cache_service.rb', line 7

def user_ids
  @user_ids
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/users/update_todo_count_cache_service.rb', line 14

def execute
  user_ids.each_slice(QUERY_BATCH_SIZE) do |user_ids_batch|
    todo_counts = Todo.for_user(user_ids_batch).pending_count_by_user_id

    user_ids_batch.each do |user_id|
      count = todo_counts.fetch(user_id, 0)

      Rails.cache.write(
        ['users', user_id, "todos_pending_count"],
        count,
        expires_in: User::COUNT_CACHE_VALIDITY_PERIOD
      )
    end
  end
end