Skip to content
Snippets Groups Projects

Handle warm cache situations in Maven virtual registry

Merged David Fernandez requested to merge 467983-cache-logic-warm-cache into master
10 files
+ 336
37
Compare changes
  • Side-by-side
  • Inline
Files
10
@@ -36,12 +36,42 @@ class CachedResponse < ApplicationRecord
fuzzy_search(query, [:relative_path], use_minimum_char_limit: false)
end
# create or update a cached response identified by the upstream, group_id and relative_path
# Given that we have chances that this function is not executed in isolation, we can't use
# safe_find_or_create_by.
# We are using the check existence and rescue alternative.
def self.retriable_create_or_update_by(upstream:, group_id:, relative_path:, attributes: {})
record = find_or_initialize_by(upstream: upstream, group_id: group_id, relative_path: relative_path)
attributes[:downloads_count] = record.downloads_count + 1 if record.persisted?
record.update!(**attributes)
record
rescue ActiveRecord::RecordInvalid => invalid
# in case of a race condition, retry the block
retry if invalid.record&.errors&.of_kind?(:relative_path, :taken)
# otherwise, bubble up the error
raise
end
def filename
return unless relative_path
File.basename(relative_path)
end
def can_be_used?
return false unless upstream.registry
Time.zone.now.before?(upstream_checked_at + upstream.registry.cache_validity_hours.hours)
end
def bump_statistics(include_upstream_checked_at: false)
now = Time.zone.now
updates = { downloaded_at: now, downloads_count: downloads_count + 1 }
updates[:upstream_checked_at] = now if include_upstream_checked_at
update_columns(**updates)
end
private
def set_object_storage_key
Loading