Class: Ci::RetentionPolicies::PipelineDeletionCutoffCache

Inherits:
Object
  • Object
show all
Defined in:
lib/ci/retention_policies/pipeline_deletion_cutoff_cache.rb

Overview

Cache the last deletion timestamp for pipeline cleanup operations per project and status.

This class provides Redis-based storage for caching the last processed pipeline’s created_at timestamp for a given project grouped by status to avoid querying of old data, thereby improving performance.

The Redis key includes both project ID and the project’s pipeline retention setting to ensure cache invalidation when retention policies change.

Examples:

Cache timestamps for multiple statuses

PipelineDeletionCutoffCache.new(project: Project.first).write(
  values: {
    'success' => Time.current,
    'failed' => 1.hour.ago,
    'canceled' => 2.hours.ago
  }
)

Clear all timestamps for a project

PipelineDeletionCutoffCache.new(project: Project.first).clear

Read all cached timestamps for a project

PipelineDeletionCutoffCache.new(project: Project.first).read
# => {"success" => 2024-11-05 12:00:00 UTC, "failed" => 2024-11-05 11:00:00 UTC}

Constant Summary collapse

REDIS_KEY_FMT =
'ci_old_pipelines_removal_cache_%{config}:{%{project_id}}'
REDIS_KEY_TTL =

to prevent stale data

1.week.to_i

Instance Method Summary collapse

Constructor Details

#initialize(project:) ⇒ PipelineDeletionCutoffCache

Returns a new instance of PipelineDeletionCutoffCache.



33
34
35
# File 'lib/ci/retention_policies/pipeline_deletion_cutoff_cache.rb', line 33

def initialize(project:)
  @project = project
end

Instance Method Details

#clearObject



58
59
60
61
62
# File 'lib/ci/retention_policies/pipeline_deletion_cutoff_cache.rb', line 58

def clear
  with_redis do |redis|
    redis.del(key)
  end
end

#readHash<String, Time>

Returns Hash of status => timestamp pairs, empty hash if no data.

Returns:

  • (Hash<String, Time>)

    Hash of status => timestamp pairs, empty hash if no data



48
49
50
51
52
53
54
55
56
# File 'lib/ci/retention_policies/pipeline_deletion_cutoff_cache.rb', line 48

def read
  with_redis do |redis|
    Gitlab::Json.parse(redis.get(key))
                .to_h
                .transform_values { |timestamp| Time.iso8601(timestamp) }
  end
rescue JSON::ParserError
  {}
end

#write(values) ⇒ String?

Returns ‘OK’ if successful, nil otherwise.

Parameters:

  • values (Hash<String, Time>)

    Hash of status => timestamp pairs

Returns:

  • (String, nil)

    ‘OK’ if successful, nil otherwise



39
40
41
42
43
44
45
# File 'lib/ci/retention_policies/pipeline_deletion_cutoff_cache.rb', line 39

def write(values)
  return if values.empty?

  with_redis do |redis|
    redis.set(key, values.compact.to_json, ex: REDIS_KEY_TTL)
  end
end