[PHP-WEBMASTER] [web-news] master: Set a cache time for articles depending on age; and 3 minutes otherwise

Author: Derick Rethans (derickr)
Date: 2026-10-05T17:07:22+01:00

Commit: Set a cache time for articles depending on age; and 3 minutes otherwise · php/web-news@fd6627e · GitHub
Raw diff: https://github.com/php/web-news/commit/fd6627ef368ad0453be451aba98c0f7463af6673.diff

Set a cache time for articles depending on age; and 3 minutes otherwise

Changed paths:
  M article.php
  M lib/common.php

Diff:

diff --git a/article.php b/article.php
index a9d5760..ff5aec8 100644
--- a/article.php
+++ b/article.php
@@ -32,6 +32,24 @@
         $rawReferences = $mail['headers']['in-reply-to'];
     }

+ if (array_key_exists('received', $mail)) {
+ $now = new DateTimeImmutable();
+ $messageTime = new DateTimeImmutable( $mail['received'] );
+ $diff = $now->diff($messageTime);
+
+ if ($diff->y > 1) {
+ $cacheTimeSeconds = 60 * 60 * 24;
+ } else if ($diff->m > 6) {
+ $cacheTimeSeconds = 60 * 60 * 12;
+ } else if ($diff->m > 1) {
+ $cacheTimeSeconds = 60 * 30;
+ } else {
+ $cacheTimeSeconds = 60 * 3;
+ }
+ } else {
+ $cacheTimeSeconds = 60 * 3;
+ }
+
     $references = [];
     foreach ($rawReferences as $ref) {
         $matches = [];
@@ -59,7 +77,7 @@
     error($e->getMessage());
}

-head("{$group}: " . format_title($mail['headers']['subject'], 'utf-8'));
+head("{$group}: " . format_title($mail['headers']['subject'], 'utf-8'), $cacheTimeSeconds);
$cleanGroupUrl = urlencode($group);
$cleanGroup = clean($group);

diff --git a/lib/common.php b/lib/common.php
index 5dd2ac5..b94e4bb 100644
--- a/lib/common.php
+++ b/lib/common.php
@@ -102,9 +102,10 @@ function posttohost($url, $data)
     return file_get_contents($url, false, $ctx);
}

-function head($title = "PHP Mailing Lists (PHP News)")
+function head($title = "PHP Mailing Lists (PHP News)", int $cacheTimeSeconds = 180)
{
     header("Content-type: text/html; charset=utf-8");
+ header("Cache-Control: public, max-age={$cacheTimeSeconds}");

     ?>
<!doctype html>