summaryrefslogtreecommitdiff
path: root/app/services/users
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-11-15 15:47:10 +0100
committerYorick Peterse <yorickpeterse@gmail.com>2017-11-16 14:59:38 +0100
commit3e561736b2eb4866b75c57c01769586f058a2f8d (patch)
tree6e136d37d558b14bfbc2505f8b8f564fa96eab6f /app/services/users
parent81e94ce1761b48b73be2a8d71938dfe934921e35 (diff)
downloadgitlab-ce-3e561736b2eb4866b75c57c01769586f058a2f8d.tar.gz
Cache the number of user SSH keyscache-user-keys-count
By caching the number of personal SSH keys we reduce the number of queries necessary on pages such as ProjectsController#show (which can end up querying this data multiple times). The cache is refreshed/flushed whenever an SSH key is added, removed, or when a user is removed.
Diffstat (limited to 'app/services/users')
-rw-r--r--app/services/users/keys_count_service.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/app/services/users/keys_count_service.rb b/app/services/users/keys_count_service.rb
new file mode 100644
index 00000000000..f82d27eded9
--- /dev/null
+++ b/app/services/users/keys_count_service.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+module Users
+ # Service class for getting the number of SSH keys that belong to a user.
+ class KeysCountService < BaseCountService
+ attr_reader :user
+
+ # user - The User for which to get the number of SSH keys.
+ def initialize(user)
+ @user = user
+ end
+
+ def relation_for_count
+ user.keys
+ end
+
+ def raw?
+ # Since we're storing simple integers we don't need all of the additional
+ # Marshal data Rails includes by default.
+ true
+ end
+
+ def cache_key
+ "users/key-count-service/#{user.id}"
+ end
+ end
+end