Skip to content
← Back to Snippets
Code

Cache Warmup Route Plan

Prioritizes cache warmup routes by traffic value, invalidation reason, and estimated build cost.

Purpose

Prioritizes cache warmup routes by traffic value, invalidation reason, and estimated build cost.

Snippet details

ContextCacheLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Cache Warmup Route Plan.
 *
 * Purpose:
 * Prioritizes cache warmup routes by traffic value, invalidation reason, and estimated build cost.
 *
 * @param array $routes Routes with traffic, cost, and reason values.
 * @return array Warmup routes sorted by priority score.
 */
function ogSnippetCacheWarmupRoutePlan(array $routes): array {
	$planned = array();
	foreach ($routes as $route) {
		$traffic = 0;
		if (isset($route['traffic'])) {
			$traffic = (int) $route['traffic'];
		}

		$cost = 1;
		if (isset($route['cost'])) {
			$cost = (int) $route['cost'];
		}
		if ($cost < 1) {
			$cost = 1;
		}
		$score = (int) floor($traffic / $cost);
		$reason = 'changed';
		if (isset($route['reason'])) {
			$reason = (string) $route['reason'];
		}
		$planned[] = array('slug' => (string) $route['slug'], 'score' => $score, 'reason' => $reason);
	}
	usort($planned, function (array $left, array $right): int {
		return $right['score'] <=> $left['score'];
	});
	return $planned;
}

$warmup = ogSnippetCacheWarmupRoutePlan(array(array('slug' => 'ring-builder-index', 'traffic' => 900, 'cost' => 3), array('slug' => 'cantina-menu', 'traffic' => 120, 'cost' => 1)));
echo $warmup[0]['slug'];