Class: BaseCountService
- Inherits:
-
Object
show all
- Defined in:
- app/services/base_count_service.rb
Overview
Base class for services that count a single resource such as the number of issues for a project.
Instance Method Summary
collapse
Instance Method Details
#cache_key ⇒ Object
37
38
39
|
# File 'app/services/base_count_service.rb', line 37
def cache_key
raise NotImplementedError, 'cache_key must be implemented and return a String, Array, or Hash'
end
|
#cache_options ⇒ Object
subclasses can override to add any specific options, such as
super.merge({ expires_in: 5.minutes })
43
44
45
|
# File 'app/services/base_count_service.rb', line 43
def cache_options
{ raw: raw? }
end
|
#count ⇒ Object
13
14
15
|
# File 'app/services/base_count_service.rb', line 13
def count
Rails.cache.fetch(cache_key, cache_options) { uncached_count }.to_i
end
|
#count_stored? ⇒ Boolean
17
18
19
|
# File 'app/services/base_count_service.rb', line 17
def count_stored?
Rails.cache.read(cache_key).present?
end
|
#delete_cache ⇒ Object
29
30
31
|
# File 'app/services/base_count_service.rb', line 29
def delete_cache
::Gitlab::Cache.delete(cache_key)
end
|
#raw? ⇒ Boolean
33
34
35
|
# File 'app/services/base_count_service.rb', line 33
def raw?
false
end
|
#refresh_cache(&block) ⇒ Object
21
22
23
|
# File 'app/services/base_count_service.rb', line 21
def refresh_cache(&block)
update_cache_for_key(cache_key, &block)
end
|
#relation_for_count ⇒ Object
6
7
8
9
10
11
|
# File 'app/services/base_count_service.rb', line 6
def relation_for_count
raise(
NotImplementedError,
'"relation_for_count" must be implemented and return an ActiveRecord::Relation'
)
end
|
#uncached_count ⇒ Object
25
26
27
|
# File 'app/services/base_count_service.rb', line 25
def uncached_count
relation_for_count.count
end
|
#update_cache_for_key(key, &block) ⇒ Object
47
48
49
|
# File 'app/services/base_count_service.rb', line 47
def update_cache_for_key(key, &block)
Rails.cache.write(key, block ? yield : uncached_count, raw: raw?)
end
|