Add class to perform all ETag cache invalidation

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

  • Close this issue

The following discussion from !18950 (merged) should be addressed:

  • @DylanGriffith started a discussion: (+2 comments)

    I'm not sure I love the fact that we're using routing helpers/concepts deep in our model layer. Did you consider any other approaches here? Nothing stands out to me but I wonder if there could be some inversion of control here that would make this model know less about higher level concepts like EtagCaching/routing.

Invalidation of ETag caching involves touching the ETag store, where the key is a route. This means that we currently have calls to routing helpers in model methods that do ETag caching invalidation.

For example:

::Gitlab::EtagCaching::Store.new.tap do |store|
  store.touch(
    ::Gitlab::Routing.url_helpers.k8s_pod_logs_project_environment_path(
      environment.project,
      environment,
      opts['pod_name'],
      opts['container_name'],
      format: :json
    )
  )
end

One option to improve this situation is to add a class under Gitlab::EtagCaching (say Gitlab::EtagCaching::Invalidator) which can have class methods for invalidating ETag caches.

module Gitlab
  module EtagCaching
    class Invalidator
      def self.invalidate_k8s_pod_logs_cache(environment, pod_name, container_name)
        ::Gitlab::EtagCaching::Store.new.tap do |store|
          store.touch(
            ::Gitlab::Routing.url_helpers.k8s_pod_logs_project_environment_path(
              environment.project,
              environment,
              opts['pod_name'],
              opts['container_name'],
              format: :json
            )
          )
        end
      end
    end
  end
end

This would have the following benefits:

  • The invalidation is located near the definition of the route.
  • The model doesn't need to know how the invalidation happens, it just needs to call this method.
  • For someone reading the code, it becomes easy to find where the invalidation happens by simply searching where this method is called.
  • Our code (including a number of models) is peppered with calls to Gitlab::EtagCaching::Store to invalidate caches. They can, instead, all be in this one location.
Edited Jul 02, 2025 by 🤖 GitLab Bot 🤖
Assignee Loading
Time tracking Loading