28use Wikimedia\AtEase\AtEase;
38 private const CONSTRUCTOR_OPTIONS = [
39 MainConfigNames::CacheEpoch,
40 MainConfigNames::FileCacheDepth,
41 MainConfigNames::FileCacheDirectory,
42 MainConfigNames::MimeType,
43 MainConfigNames::UseGzip,
57 private const MISS_FACTOR = 15;
58 private const MISS_TTL_SEC = 3600;
62 self::CONSTRUCTOR_OPTIONS,
63 MediaWikiServices::getInstance()->getMainConfig()
65 $this->mUseGzip = (bool)$this->options->get( MainConfigNames::UseGzip );
73 return $this->options->get( MainConfigNames::FileCacheDirectory );
87 if ( $this->mFilePath !==
null ) {
92 # Build directories (methods include the trailing "/")
94 # Avoid extension confusion
95 $key = str_replace(
'.',
'%2E', urlencode( $this->mKey ) );
96 # Build the full file path
97 $this->mFilePath =
"{$dir}/{$subDirs}{$key}.{$this->mExt}";
99 $this->mFilePath .=
'.gz';
110 $this->mCached ??= is_file( $this->
cachePath() );
120 $timestamp = filemtime( $this->
cachePath() );
122 return ( $timestamp !==
false )
134 $cacheEpoch = $this->options->get( MainConfigNames::CacheEpoch );
141 $good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime );
143 ": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " .
wfBoolToStr( $good ) );
162 $fh = gzopen( $this->
cachePath(),
'rb' );
164 return stream_get_contents( $fh );
166 return file_get_contents( $this->
cachePath() );
177 $text = gzencode( $text );
181 if ( !file_put_contents( $this->
cachePath(), $text, LOCK_EX ) ) {
183 $this->mCached =
null;
188 $this->mCached =
true;
198 AtEase::suppressWarnings();
200 AtEase::restoreWarnings();
201 $this->mCached =
false;
220 return $this->mType .
'/';
229 $fileCacheDepth = $this->options->get( MainConfigNames::FileCacheDepth );
232 if ( $fileCacheDepth > 0 ) {
233 $hash = md5( $this->mKey );
234 for ( $i = 1; $i <= $fileCacheDepth; $i++ ) {
235 $subdir .= substr( $hash, 0, $i ) .
'/';
248 if ( mt_rand( 0, self::MISS_FACTOR - 1 ) == 0 ) {
249 # Get a large IP range that should include the user even if that
250 # person's IP address changes
251 $ip = $request->
getIP();
252 if ( !IPUtils::isValid( $ip ) ) {
256 $ip = IPUtils::isIPv6( $ip )
257 ? IPUtils::sanitizeRange(
"$ip/32" )
258 : IPUtils::sanitizeRange(
"$ip/16" );
260 # Bail out if a request already came from this range...
261 $cache = ObjectCache::getLocalClusterInstance();
262 $key = $cache->makeKey( static::class,
'attempt', $this->mType, $this->mKey, $ip );
263 if ( !$cache->add( $key, 1, self::MISS_TTL_SEC ) ) {
267 # Increment the number of cache misses...
268 $cache->incrWithInit( $this->
cacheMissKey( $cache ), self::MISS_TTL_SEC );
277 $cache = ObjectCache::getLocalClusterInstance();
279 return self::MISS_FACTOR * $cache->get( $this->
cacheMissKey( $cache ) );
287 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.
makeKey( $keygroup,... $components)
Make a cache key from the given components, in the default keyspace.
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.