]> git.bts.cx Git - cx.git/commitdiff
Added cache support
authorBen Sherratt <redacted>
Sat, 4 Jul 2026 17:07:00 +0000 (18:07 +0100)
committerBen Sherratt <redacted>
Sat, 4 Jul 2026 17:07:00 +0000 (18:07 +0100)
cx/lib/template.php
public/.htaccess

index 6826d162ea22b1d2611073016e54f9f650baf07a..004c2abccf5b96eb9afd74a25239dc717d3a58a2 100644 (file)
@@ -12,7 +12,7 @@ function cx_template_has_content($class, $path) {
        return file_exists(cx_template_content_path($class, $path));
 }
 
        return file_exists(cx_template_content_path($class, $path));
 }
 
-function cx_template_output_content($class, $path) {
+function cx_template_output_content($class, $path, $cache_age = 30 * 86400) {
        $full_path = cx_template_content_path($class, $path);
 
        $extension = pathinfo($path, PATHINFO_EXTENSION);
        $full_path = cx_template_content_path($class, $path);
 
        $extension = pathinfo($path, PATHINFO_EXTENSION);
@@ -25,8 +25,22 @@ function cx_template_output_content($class, $path) {
                //finfo_close($finfo); - Deprecated
        }
 
                //finfo_close($finfo); - Deprecated
        }
 
+       $file_size = filesize($full_path);
+       $file_modified_time = filemtime($full_path);
+
        header('Content-Type: ' . $mime_type);
        header('Content-Type: ' . $mime_type);
-       header('Content-Length: ' . filesize($full_path));
+       header('Content-Length: ' . $file_size);
+       header('Cache-Control: public, max-age=604800');
+       header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_age) . ' GMT');
+       header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $file_modified_time) . ' GMT');
+
+       if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
+               $request_modification = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
+               if ($request_modification >= $file_modified_time) {
+                       http_response_code(304);
+                       return;
+               }
+       }
 
        $fp = fopen($full_path, 'rb');
        fpassthru($fp);
 
        $fp = fopen($full_path, 'rb');
        fpassthru($fp);
index 3089e13188b80b584082ce85d6669c570141303d..fdf1802d55fb18909b278e6efbda36b6dff4f288 100644 (file)
@@ -1,11 +1,11 @@
-# BEGIN CX 
+# BEGIN CX
 
 
-RewriteEngine On 
-RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 
-RewriteBase / 
-RewriteRule ^index\.php$ - [L] 
-RewriteCond %{REQUEST_FILENAME} !-f 
-RewriteCond %{REQUEST_FILENAME} !-d 
-RewriteRule . /index.php [L] 
+RewriteEngine On
+RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
+RewriteBase /
+RewriteRule ^index\.php$ - [L]
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteRule . /index.php [L]
 
 # END CX
\ No newline at end of file
 
 # END CX
\ No newline at end of file