diff options
Diffstat (limited to 'app/services/users/update_todo_count_cache_service.rb')
-rw-r--r-- | app/services/users/update_todo_count_cache_service.rb | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/app/services/users/update_todo_count_cache_service.rb b/app/services/users/update_todo_count_cache_service.rb index 03ab66bd64a..3407b22e355 100644 --- a/app/services/users/update_todo_count_cache_service.rb +++ b/app/services/users/update_todo_count_cache_service.rb @@ -4,31 +4,34 @@ module Users class UpdateTodoCountCacheService < BaseService QUERY_BATCH_SIZE = 10 - attr_reader :users + attr_reader :user_ids - # users - An array of User objects - def initialize(users) - @users = users + # user_ids - An array of User IDs + def initialize(user_ids) + @user_ids = user_ids end def execute - users.each_slice(QUERY_BATCH_SIZE) do |users_batch| - todo_counts = Todo.for_user(users_batch).count_grouped_by_user_id_and_state + user_ids.each_slice(QUERY_BATCH_SIZE) do |user_ids_batch| + todo_counts = Todo.for_user(user_ids_batch).count_grouped_by_user_id_and_state - users_batch.each do |user| - update_count_cache(user, todo_counts, :done) - update_count_cache(user, todo_counts, :pending) + user_ids_batch.each do |user_id| + update_count_cache(user_id, todo_counts, :done) + update_count_cache(user_id, todo_counts, :pending) end end end private - def update_count_cache(user, todo_counts, state) - count = todo_counts.fetch([user.id, state.to_s], 0) - expiration_time = user.count_cache_validity_period + def update_count_cache(user_id, todo_counts, state) + count = todo_counts.fetch([user_id, state.to_s], 0) - Rails.cache.write(['users', user.id, "todos_#{state}_count"], count, expires_in: expiration_time) + Rails.cache.write( + ['users', user_id, "todos_#{state}_count"], + count, + expires_in: User::COUNT_CACHE_VALIDITY_PERIOD + ) end end end |