Class: Groups::CountService

Inherits:
BaseCountService show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/groups/count_service.rb

Constant Summary collapse

VERSION =
1
CACHED_COUNT_THRESHOLD =
1000
EXPIRATION_TIME =
24.hours

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from BaseCountService

#count_stored?, #delete_cache, #raw?, #refresh_cache, #uncached_count, #update_cache_for_key

Constructor Details

#initialize(group, user = nil) ⇒ CountService

Returns a new instance of CountService.


13
14
15
16
# File 'app/services/groups/count_service.rb', line 13

def initialize(group, user = nil)
  @group = group
  @user = user
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.


11
12
13
# File 'app/services/groups/count_service.rb', line 11

def group
  @group
end

#userObject (readonly)

Returns the value of attribute user.


11
12
13
# File 'app/services/groups/count_service.rb', line 11

def user
  @user
end

Instance Method Details

#cache_key(key_name = nil) ⇒ Object


38
39
40
41
42
# File 'app/services/groups/count_service.rb', line 38

def cache_key(key_name = nil)
  key_name ||= cache_key_name

  ['groups', "#{issuable_key}_count_service", VERSION, group.id, key_name]
end

#countObject


18
19
20
21
22
23
# File 'app/services/groups/count_service.rb', line 18

def count
  cached_count = Rails.cache.read(cache_key)
  return cached_count unless cached_count.blank?

  refresh_cache_over_threshold
end

#refresh_cache_over_threshold(key = nil) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/groups/count_service.rb', line 25

def refresh_cache_over_threshold(key = nil)
  key ||= cache_key
  new_count = uncached_count

  if new_count > CACHED_COUNT_THRESHOLD
    update_cache_for_key(key) { new_count }
  else
    delete_cache
  end

  new_count
end