MediaWiki REL1_35
FileCacheBase.php
Go to the documentation of this file.
1<?php
24use Wikimedia\IPUtils;
25
31abstract class FileCacheBase {
32 protected $mKey;
33 protected $mType = 'object';
34 protected $mExt = 'cache';
35 protected $mFilePath;
36 protected $mUseGzip;
37 /* lazy loaded */
38 protected $mCached;
39
40 /* @todo configurable? */
41 private const MISS_FACTOR = 15; // log 1 every MISS_FACTOR cache misses
42 private const MISS_TTL_SEC = 3600; // how many seconds ago is "recent"
43
44 protected function __construct() {
45 global $wgUseGzip;
46
47 $this->mUseGzip = (bool)$wgUseGzip;
48 }
49
54 final protected function baseCacheDirectory() {
56
58 }
59
64 abstract protected function cacheDirectory();
65
70 protected function cachePath() {
71 if ( $this->mFilePath !== null ) {
72 return $this->mFilePath;
73 }
74
75 $dir = $this->cacheDirectory();
76 # Build directories (methods include the trailing "/")
77 $subDirs = $this->typeSubdirectory() . $this->hashSubdirectory();
78 # Avoid extension confusion
79 $key = str_replace( '.', '%2E', urlencode( $this->mKey ) );
80 # Build the full file path
81 $this->mFilePath = "{$dir}/{$subDirs}{$key}.{$this->mExt}";
82 if ( $this->useGzip() ) {
83 $this->mFilePath .= '.gz';
84 }
85
86 return $this->mFilePath;
87 }
88
93 public function isCached() {
94 if ( $this->mCached === null ) {
95 $this->mCached = file_exists( $this->cachePath() );
96 }
97
98 return $this->mCached;
99 }
100
105 public function cacheTimestamp() {
106 $timestamp = filemtime( $this->cachePath() );
107
108 return ( $timestamp !== false )
109 ? wfTimestamp( TS_MW, $timestamp )
110 : false;
111 }
112
119 public function isCacheGood( $timestamp = '' ) {
120 global $wgCacheEpoch;
121
122 if ( !$this->isCached() ) {
123 return false;
124 }
125
126 $cachetime = $this->cacheTimestamp();
127 $good = ( $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime );
128 wfDebug( __METHOD__ .
129 ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good" );
130
131 return $good;
132 }
133
138 protected function useGzip() {
139 return $this->mUseGzip;
140 }
141
146 public function fetchText() {
147 if ( $this->useGzip() ) {
148 $fh = gzopen( $this->cachePath(), 'rb' );
149
150 return stream_get_contents( $fh );
151 } else {
152 return file_get_contents( $this->cachePath() );
153 }
154 }
155
161 public function saveText( $text ) {
162 if ( $this->useGzip() ) {
163 $text = gzencode( $text );
164 }
165
166 $this->checkCacheDirs(); // build parent dir
167 if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX ) ) {
168 wfDebug( __METHOD__ . "() failed saving " . $this->cachePath() );
169 $this->mCached = null;
170
171 return false;
172 }
173
174 $this->mCached = true;
175
176 return $text;
177 }
178
183 public function clearCache() {
184 Wikimedia\suppressWarnings();
185 unlink( $this->cachePath() );
186 Wikimedia\restoreWarnings();
187 $this->mCached = false;
188 }
189
194 protected function checkCacheDirs() {
195 wfMkdirParents( dirname( $this->cachePath() ), null, __METHOD__ );
196 }
197
205 protected function typeSubdirectory() {
206 return $this->mType . '/';
207 }
208
214 protected function hashSubdirectory() {
215 global $wgFileCacheDepth;
216
217 $subdir = '';
218 if ( $wgFileCacheDepth > 0 ) {
219 $hash = md5( $this->mKey );
220 for ( $i = 1; $i <= $wgFileCacheDepth; $i++ ) {
221 $subdir .= substr( $hash, 0, $i ) . '/';
222 }
223 }
224
225 return $subdir;
226 }
227
233 public function incrMissesRecent( WebRequest $request ) {
234 if ( mt_rand( 0, self::MISS_FACTOR - 1 ) == 0 ) {
235 # Get a large IP range that should include the user even if that
236 # person's IP address changes
237 $ip = $request->getIP();
238 if ( !IPUtils::isValid( $ip ) ) {
239 return;
240 }
241
242 $ip = IPUtils::isIPv6( $ip )
243 ? IPUtils::sanitizeRange( "$ip/32" )
244 : IPUtils::sanitizeRange( "$ip/16" );
245
246 # Bail out if a request already came from this range...
247 $cache = ObjectCache::getLocalClusterInstance();
248 $key = $cache->makeKey( static::class, 'attempt', $this->mType, $this->mKey, $ip );
249 if ( !$cache->add( $key, 1, self::MISS_TTL_SEC ) ) {
250 return; // possibly the same user
251 }
252
253 # Increment the number of cache misses...
254 $cache->incrWithInit( $this->cacheMissKey( $cache ), self::MISS_TTL_SEC );
255 }
256 }
257
262 public function getMissesRecent() {
263 $cache = ObjectCache::getLocalClusterInstance();
264
265 return self::MISS_FACTOR * $cache->get( $this->cacheMissKey( $cache ) );
266 }
267
272 protected function cacheMissKey( BagOStuff $cache ) {
273 return $cache->makeKey( static::class, 'misses', $this->mType, $this->mKey );
274 }
275}
$wgCacheEpoch
Set this to current time to invalidate all prior cached pages.
$wgFileCacheDepth
Depth of the subdirectory hierarchy to be created under $wgFileCacheDirectory.
$wgUseGzip
When using the file cache, we can store the cached HTML gzipped to save disk space.
$wgFileCacheDirectory
Directory where the cached page will be saved.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:71
Base class for data storage in the file system.
isCacheGood( $timestamp='')
Check if up to date cache file exists.
useGzip()
Check if the cache is gzipped.
cachePath()
Get the path to the cache file.
saveText( $text)
Save and compress text to the cache.
fetchText()
Get the uncompressed text from the cache.
hashSubdirectory()
Return relative multi-level hash subdirectory (with trailing slash) or the empty string if not $wgFil...
getMissesRecent()
Roughly gets the cache misses in the last hour by unique visitors.
cacheDirectory()
Get the base cache directory (not specific to this file)
checkCacheDirs()
Create parent directors of $this->cachePath()
cacheTimestamp()
Get the last-modified timestamp of the cache file.
clearCache()
Clear the cache for this page.
typeSubdirectory()
Get the cache type subdirectory (with trailing slash) An extending class could use that method to alt...
cacheMissKey(BagOStuff $cache)
baseCacheDirectory()
Get the base file cache directory.
incrMissesRecent(WebRequest $request)
Roughly increments the cache misses in the last hour by unique visitors.
isCached()
Check if the cache file exists.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getIP()
Work out the IP address based on various globals For trusted proxies, use the XFF client IP (first of...
$cache
Definition mcc.php:33