handles_overview_caching.rb 1.2 KB

1234567891011121314151617181920212223242526272829
  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. overview.condition.values.any? { |field_condition| field_condition['pre_condition']&.include?('current_user') }
  20. end
  21. end
  22. end