handles_overview_caching.rb 1.2 KB

123456789101112131415161718192021222324252627282930
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Concerns::HandlesOverviewCaching
  3. extend ActiveSupport::Concern
  4. included do
  5. def object_cache_key(overview)
  6. # context_key seems to only work in field resolvers, so build a custom cache key here.
  7. # query_cache_key of graphql-fragment_cache already has all arguments and selected fields covered,
  8. # so here we only need to cover the additional dynamic parts.
  9. cache_key_parts = [
  10. "overviewLastUpdate:#{overview.updated_at.to_time.to_i}",
  11. "groupPermissions:#{context.current_user.group_ids_access(:overview).sort}",
  12. ]
  13. # Cache overview contents by permission set by default, so that users with same permissions use the same cache.
  14. # Only include the currentUserId for overviews which refer to the current user.
  15. cache_key_parts << "currentUserId:#{context.current_user.id}" if overview_is_personalized?(overview)
  16. cache_key_parts.join('-')
  17. end
  18. def overview_is_personalized?(overview)
  19. # Stringify so that it works both with simple and expert mode conditions.
  20. overview.condition.to_s.include?('current_user')
  21. end
  22. end
  23. end