Stale Cache File Purge Plan
Plans stale cache-file deletions by age, extension, size, and protected filename rules without deleting files.
Purpose
Plans stale cache-file deletions by age, extension, size, and protected filename rules without deleting files.
Snippet details
ContextFileLevelAdvancedCopy-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.
*/
/**
* Stale Cache File Purge Plan.
*
* Purpose:
* Plans stale cache-file deletions by age, extension, size, and protected filename rules without deleting files.
*
* @param array $cache_files File rows with path, modified_at, bytes, and extension.
* @param int $max_age_seconds Maximum allowed age in seconds.
* @return array Purge plan without deleting files.
*/
function ogSnippetStaleCacheFilePurgePlan(array $cache_files, int $max_age_seconds): array {
$now = time();
$plan = array('purge' => array(), 'keep' => array());
foreach ($cache_files as $file_row) {
$path = (string) $file_row['path'];
$modified_at = (int) $file_row['modified_at'];
$extension = strtolower((string) $file_row['extension']);
$age = $now - $modified_at;
$protected = false;
if (strpos($path, '.htaccess') !== false || strpos($path, 'index.html') !== false) {
$protected = true;
}
if ($age > $max_age_seconds && $extension === 'cache' && $protected === false) {
$plan['purge'][] = array('path' => $path, 'age_seconds' => $age);
} else {
$plan['keep'][] = array('path' => $path, 'reason' => 'not_stale_or_protected');
}
}
return $plan;
}
$cache_rows = array(array('path' => '/tmp/ring-gate.cache', 'modified_at' => time() - 90000, 'extension' => 'cache'));
$purge_plan = ogSnippetStaleCacheFilePurgePlan($cache_rows, 86400);
echo count($purge_plan['purge']);