gitlab-org--gitlab-foss/app/services/base_count_service.rb
Luke Duncalfe a0b14c40dc CE-specific changes for designs user_notes_count
Notes call `#after_note_created` and `#after_note_destroyed` on their
noteable in callbacks, so the noteable can perform tasks particular to
them, like cache expiry.

This is in preparation of the EE-specific class
`DesignManagement::Design` clearing its `user_notes_count` cache when
its note are created or destroyed.

Refactoring Rspec behaviour testing of a counter caching service into a
shared example.

https://gitlab.com/gitlab-org/gitlab-ee/issues/13353
2019-08-27 20:50:26 +00:00

50 lines
1.1 KiB
Ruby

# frozen_string_literal: true
# Base class for services that count a single resource such as the number of
# issues for a project.
class BaseCountService
def relation_for_count
raise(
NotImplementedError,
'"relation_for_count" must be implemented and return an ActiveRecord::Relation'
)
end
def count
Rails.cache.fetch(cache_key, cache_options) { uncached_count }.to_i
end
def count_stored?
Rails.cache.read(cache_key).present?
end
def refresh_cache(&block)
update_cache_for_key(cache_key, &block)
end
def uncached_count
relation_for_count.count
end
def delete_cache
Rails.cache.delete(cache_key)
end
def raw?
false
end
def cache_key
raise NotImplementedError, 'cache_key must be implemented and return a String, Array, or Hash'
end
# subclasses can override to add any specific options, such as
# super.merge({ expires_in: 5.minutes })
def cache_options
{ raw: raw? }
end
def update_cache_for_key(key, &block)
Rails.cache.write(key, block_given? ? yield : uncached_count, raw: raw?)
end
end