Add small HtmlCacheUpdater service class to normalize purging code
[lhc/web/wiklou.git] / includes / cache / HtmlCacheUpdater.php
1 <?php
2 /**
3 * HTML/file cache invalidation of cacheable variant/action URLs for a page
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Class to invalidate the HTML/file cache of cacheable variant/action URLs for a page
26 *
27 * @ingroup Cache
28 * @since 1.34
29 */
30 class HtmlCacheUpdater {
31 /** @var int Purge after the main transaction round and respect $wgCdnReboundPurgeDelay */
32 const ISOLATION_AND_LAG_AWARE = 1;
33 /** @var int Purge immediately and only once (ignore $wgCdnReboundPurgeDelay) */
34 const IMMEDIATE_WITHOUT_REBOUND = 2;
35
36 /**
37 * Purge CDN/HTMLFileCache for a URL, Title, or iteratable of URL or Title entries
38 *
39 * String entries will be treated as URLs to be purged from the CDN layer.
40 * For Title entries, all cacheable canonical URLs associated with the page
41 * will be purged from the CDN and HTMLFileCache.
42 *
43 * The cache purges are queued as PRESEND deferred updates so that they run after the
44 * main database transaction round of LBFactory. This reduces the chance of race conditions
45 * where a stale value is re-populated before commit. Depending on $wgCdnReboundPurgeDelay,
46 * a secondary set of purges might be issued several seconds later through the use of a
47 * delayed job. This is used to mitigate the effects of DB replication lag as well as
48 * multiple layers of CDN proxies. All deferred CDN purges are combined and de-duplicated
49 * into a single DeferrableUpdate instance. This improves HTTP PURGE request pipelining.
50 *
51 * Use the IMMEDIATE_WITHOUT_REBOUND class constant to instantly issue the purges instead
52 * and skip the use of any secondary purges regardless of $wgCdnReboundPurgeDelay.
53 *
54 * @param Traversable|Title[]|Title|string[]|string $entries
55 * @param int $mode ISOLATION_AND_LAG_AWARE or IMMEDIATE_WITHOUT_REBOUND class constant
56 */
57 public function purge( $entries, $mode = self::ISOLATION_AND_LAG_AWARE ) {
58 $urls = [];
59 $titles = [];
60 if ( is_string( $entries ) ) {
61 $urls = [ $entries ];
62 } elseif ( $entries instanceof Title ) {
63 $titles = [ $entries ];
64 } elseif ( $entries instanceof TitleArray ) {
65 $titles = $entries; // save memory
66 } else {
67 foreach ( $entries as $entry ) {
68 if ( is_string( $entry ) ) {
69 $urls[] = $entry;
70 } else {
71 $titles[] = $entry;
72 }
73 }
74 }
75
76 if ( $mode === self::IMMEDIATE_WITHOUT_REBOUND ) {
77 HTMLFileCache::clearFileCache( $titles );
78 foreach ( $titles as $title ) {
79 /** @var Title $title */
80 $urls = array_merge( $urls, $title->getCdnUrls() );
81 }
82 CdnCacheUpdate::purge( $urls ); // purge once (no "rebound" purges)
83 } else {
84 DeferredUpdates::addUpdate(
85 HtmlFileCacheUpdate::newFromTitles( $titles ),
86 DeferredUpdates::PRESEND
87 );
88 DeferredUpdates::addUpdate(
89 CdnCacheUpdate::newFromTitles( $titles, $urls ),
90 DeferredUpdates::PRESEND
91 );
92 }
93 }
94 }