Last active
December 16, 2015 09:29
-
-
Save julp/5413714 to your computer and use it in GitHub Desktop.
Manage easily HTTP caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function LastModified(Closure $when, Closure $noncached) { | |
| $rmtime = $when(); | |
| if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { | |
| $lmtime = date_create($_SERVER['HTTP_IF_MODIFIED_SINCE']); | |
| if (FALSE !== $lmtime && $lmtime >= $rmtime) { | |
| header('HTTP/1.1 304 Not Modified', TRUE, 304); | |
| exit; | |
| } | |
| } | |
| header('Cache-Control: max-age=86400, public'); | |
| header('Last-Modified: ' . $rmtime->format(DateTime::RFC1123)); | |
| $noncached(); | |
| } | |
| function ETag(Closure $when, Closure $noncached) { | |
| $etag = md5($when()); | |
| if (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { | |
| header('HTTP/1.1 304 Not Modified', TRUE, 304); | |
| exit; | |
| } | |
| header('Cache-Control: max-age=86400, public'); | |
| header('ETag: ' . $etag); | |
| $noncached(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $dbh = new PDO(...); | |
| // articles#show | |
| LastModified( | |
| function () use ($dbh) { | |
| if (!isset($_GET['id']) || FALSE === ($id = filter_var($_GET['id'], FILTER_VALIDATE_INT))) { | |
| header('HTTP/1.1 400 Bad Request', TRUE, 400); | |
| exit; | |
| } | |
| $stmt = $dbh->prepare('SELECT updated_at FROM articles WHERE id = ?'); | |
| $stmt->execute(array($_GET['id'])); | |
| if (FALSE === ($updated_at = $stmt->fetchColumn()) { | |
| header('HTTP/1.1 404 Not Found', TRUE, 404); | |
| exit; | |
| } | |
| return new DateTime($updated_at); | |
| }, | |
| function () use ($dbh) { | |
| $stmt = $dbh->prepare('SELECT * FROM articles WHERE id = ?'); | |
| $stmt->execute(array($_GET['id'])); | |
| $article = $stmt->fetch(); | |
| printf('<h1>%s</h1>', htmlspecialchars($article['title'], ENT_QUOTES, 'UTF-8')); | |
| $sd = new Sundown($article['text']); | |
| echo $sd->to_html(); | |
| } | |
| ); | |
| // articles#index | |
| ETag( | |
| function () use ($dbh) { | |
| return implode(',', $dbh->query('SELECT id FROM articles ORDER BY updated_at DESC')->fetchAll(PDO::FETCH_COLUMN, 0)); | |
| }, | |
| function () use ($dbh) { | |
| echo '<h1>...</h1>'; | |
| foreach ($dbh->query('SELECT * FROM articles ORDER BY updated_at DESC') as $art) { | |
| echo '<div class="...">'; | |
| printf('<h2>%s</h2>', htmlspecialchars($article['title'], ENT_QUOTES, 'UTF-8')); | |
| printf('<p>%s</p>', htmlspecialchars($article['summary'], ENT_QUOTES, 'UTF-8')); | |
| echo '</div>'; | |
| } | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment