27use Wikimedia\AtEase\AtEase;
37 private const CONSTRUCTOR_OPTIONS = [
38 MainConfigNames::CacheEpoch,
39 MainConfigNames::FileCacheDepth,
40 MainConfigNames::FileCacheDirectory,
41 MainConfigNames::MimeType,
42 MainConfigNames::UseGzip,
56 private const MISS_FACTOR = 15;
57 private const MISS_TTL_SEC = 3600;
61 self::CONSTRUCTOR_OPTIONS,
62 MediaWikiServices::getInstance()->getMainConfig()
64 $this->mUseGzip = (bool)$this->options->get( MainConfigNames::UseGzip );
72 return $this->options->get( MainConfigNames::FileCacheDirectory );
86 if ( $this->mFilePath !==
null ) {
91 # Build directories (methods include the trailing "/")
93 # Avoid extension confusion
94 $key = str_replace(
'.',
'%2E', urlencode( $this->mKey ) );
95 # Build the full file path
96 $this->mFilePath =
"{$dir}/{$subDirs}{$key}.{$this->mExt}";
98 $this->mFilePath .=
'.gz';
109 if ( $this->mCached ===
null ) {
110 $this->mCached = is_file( $this->
cachePath() );
121 $timestamp = filemtime( $this->
cachePath() );
123 return ( $timestamp !==
false )
135 $cacheEpoch = $this->options->get( MainConfigNames::CacheEpoch );
142 $good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime );
144 ": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " .
wfBoolToStr( $good ) );
163 $fh = gzopen( $this->
cachePath(),
'rb' );
165 return stream_get_contents( $fh );
167 return file_get_contents( $this->
cachePath() );
178 $text = gzencode( $text );
182 if ( !file_put_contents( $this->
cachePath(), $text, LOCK_EX ) ) {
184 $this->mCached =
null;
189 $this->mCached =
true;
199 AtEase::suppressWarnings();
201 AtEase::restoreWarnings();
202 $this->mCached =
false;
221 return $this->mType .
'/';
230 $fileCacheDepth = $this->options->get( MainConfigNames::FileCacheDepth );
233 if ( $fileCacheDepth > 0 ) {
234 $hash = md5( $this->mKey );
235 for ( $i = 1; $i <= $fileCacheDepth; $i++ ) {
236 $subdir .= substr( $hash, 0, $i ) .
'/';
249 if ( mt_rand( 0, self::MISS_FACTOR - 1 ) == 0 ) {
250 # Get a large IP range that should include the user even if that
251 # person's IP address changes
252 $ip = $request->
getIP();
253 if ( !IPUtils::isValid( $ip ) ) {
257 $ip = IPUtils::isIPv6( $ip )
258 ? IPUtils::sanitizeRange(
"$ip/32" )
259 : IPUtils::sanitizeRange(
"$ip/16" );
261 # Bail out if a request already came from this range...
262 $cache = ObjectCache::getLocalClusterInstance();
263 $key =
$cache->makeKey( static::class,
'attempt', $this->mType, $this->mKey, $ip );
264 if ( !
$cache->add( $key, 1, self::MISS_TTL_SEC ) ) {
268 # Increment the number of cache misses...
278 $cache = ObjectCache::getLocalClusterInstance();
288 return $cache->makeKey( static::class,
'misses', $this->mType, $this->mKey );
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.
wfBoolToStr( $value)
Convenience function converts boolean values into "true" or "false" (string) values.
Class representing a cache/ephemeral data store.
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.
bool null $mCached
lazy loaded
A class containing constants representing the names of configuration variables.
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...