30use Wikimedia\AtEase\AtEase;
41 private const CONSTRUCTOR_OPTIONS = [
65 private const MISS_FACTOR = 15;
66 private const MISS_TTL_SEC = 3600;
70 self::CONSTRUCTOR_OPTIONS,
95 if ( $this->mFilePath !==
null ) {
100 # Build directories (methods include the trailing "/")
102 # Avoid extension confusion
103 $key = str_replace(
'.',
'%2E', urlencode( $this->mKey ) );
104 # Build the full file path
105 $this->mFilePath =
"{$dir}/{$subDirs}{$key}.{$this->mExt}";
107 $this->mFilePath .=
'.gz';
118 $this->mCached ??= is_file( $this->
cachePath() );
128 $timestamp = filemtime( $this->
cachePath() );
130 return ( $timestamp !==
false )
149 $good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime );
151 ": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " .
wfBoolToStr( $good ) );
170 $fh = gzopen( $this->
cachePath(),
'rb' );
172 return stream_get_contents( $fh );
174 return file_get_contents( $this->
cachePath() );
185 $text = gzencode( $text );
189 if ( !file_put_contents( $this->
cachePath(), $text, LOCK_EX ) ) {
191 $this->mCached =
null;
196 $this->mCached =
true;
206 AtEase::suppressWarnings();
208 AtEase::restoreWarnings();
209 $this->mCached =
false;
228 return $this->mType .
'/';
240 if ( $fileCacheDepth > 0 ) {
241 $hash = md5( $this->mKey );
242 for ( $i = 1; $i <= $fileCacheDepth; $i++ ) {
243 $subdir .= substr( $hash, 0, $i ) .
'/';
256 if ( mt_rand( 1, self::MISS_FACTOR ) == 1 ) {
257 # Get a large IP range that should include the user even if that
258 # person's IP address changes
259 $ip = $request->
getIP();
260 if ( !IPUtils::isValid( $ip ) ) {
264 $ip = IPUtils::isIPv6( $ip )
265 ? IPUtils::sanitizeRange(
"$ip/32" )
266 : IPUtils::sanitizeRange(
"$ip/16" );
268 # Bail out if a request already came from this range...
270 ->getLocalClusterInstance();
271 $key = $cache->makeKey( static::class,
'attempt', $this->mType, $this->mKey, $ip );
272 if ( !$cache->add( $key, 1, self::MISS_TTL_SEC ) ) {
276 # Increment the number of cache misses...
277 $cache->incrWithInit( $this->
cacheMissKey( $cache ), self::MISS_TTL_SEC );
287 ->getLocalClusterInstance();
289 return self::MISS_FACTOR * $cache->get( $this->
cacheMissKey( $cache ) );
297 return $cache->
makeKey( static::class,
'misses', $this->mType, $this->mKey );
302class_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()