30use Wikimedia\AtEase\AtEase;
41 private const CONSTRUCTOR_OPTIONS = [
60 private const MISS_FACTOR = 15;
61 private const MISS_TTL_SEC = 3600;
65 self::CONSTRUCTOR_OPTIONS,
90 if ( $this->mFilePath !==
null ) {
95 # Build directories (methods include the trailing "/")
97 # Avoid extension confusion
98 $key = str_replace(
'.',
'%2E', urlencode( $this->mKey ) );
99 # Build the full file path
100 $this->mFilePath =
"{$dir}/{$subDirs}{$key}.{$this->mExt}";
102 $this->mFilePath .=
'.gz';
113 $this->mCached ??= is_file( $this->
cachePath() );
123 $timestamp = filemtime( $this->
cachePath() );
125 return ( $timestamp !==
false )
144 $good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime );
146 ": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " .
wfBoolToStr( $good ) );
165 $fh = gzopen( $this->
cachePath(),
'rb' );
167 return stream_get_contents( $fh );
169 return file_get_contents( $this->
cachePath() );
180 $text = gzencode( $text );
184 if ( !file_put_contents( $this->
cachePath(), $text, LOCK_EX ) ) {
186 $this->mCached =
null;
191 $this->mCached =
true;
201 AtEase::suppressWarnings();
203 AtEase::restoreWarnings();
204 $this->mCached =
false;
223 return $this->mType .
'/';
235 if ( $fileCacheDepth > 0 ) {
236 $hash = md5( $this->mKey );
237 for ( $i = 1; $i <= $fileCacheDepth; $i++ ) {
238 $subdir .= substr( $hash, 0, $i ) .
'/';
251 if ( mt_rand( 0, self::MISS_FACTOR - 1 ) == 0 ) {
252 # Get a large IP range that should include the user even if that
253 # person's IP address changes
254 $ip = $request->
getIP();
255 if ( !IPUtils::isValid( $ip ) ) {
259 $ip = IPUtils::isIPv6( $ip )
260 ? IPUtils::sanitizeRange(
"$ip/32" )
261 : IPUtils::sanitizeRange(
"$ip/16" );
263 # Bail out if a request already came from this range...
265 ->getLocalClusterInstance();
266 $key = $cache->makeKey( static::class,
'attempt', $this->mType, $this->mKey, $ip );
267 if ( !$cache->add( $key, 1, self::MISS_TTL_SEC ) ) {
271 # Increment the number of cache misses...
272 $cache->incrWithInit( $this->
cacheMissKey( $cache ), self::MISS_TTL_SEC );
282 ->getLocalClusterInstance();
284 return self::MISS_FACTOR * $cache->get( $this->
cacheMissKey( $cache ) );
292 return $cache->
makeKey( static::class,
'misses', $this->mType, $this->mKey );
297class_alias( FileCacheBase::class,
'FileCacheBase' );
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.
A class containing constants representing the names of configuration variables.
const FileCacheDirectory
Name constant for the FileCacheDirectory setting, for use with Config::get()
const MimeType
Name constant for the MimeType setting, for use with Config::get()
const CacheEpoch
Name constant for the CacheEpoch setting, for use with Config::get()
const FileCacheDepth
Name constant for the FileCacheDepth setting, for use with Config::get()
const UseGzip
Name constant for the UseGzip setting, for use with Config::get()