18use Wikimedia\Timestamp\TimestampFormat as TS;
27 private const CONSTRUCTOR_OPTIONS = [
51 private const MISS_FACTOR = 15;
52 private const MISS_TTL_SEC = 3600;
56 self::CONSTRUCTOR_OPTIONS,
81 if ( $this->mFilePath !==
null ) {
86 # Build directories (methods include the trailing "/")
88 # Avoid extension confusion
89 $key = str_replace(
'.',
'%2E', urlencode( $this->mKey ) );
90 # Build the full file path
91 $this->mFilePath =
"{$dir}/{$subDirs}{$key}.{$this->mExt}";
93 $this->mFilePath .=
'.gz';
104 $this->mCached ??= is_file( $this->
cachePath() );
114 $timestamp = filemtime( $this->
cachePath() );
116 return ( $timestamp !==
false )
135 $good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime );
137 ": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " .
wfBoolToStr( $good ) );
156 $fh = gzopen( $this->
cachePath(),
'rb' );
158 return stream_get_contents( $fh );
160 return file_get_contents( $this->
cachePath() );
171 $text = gzencode( $text );
175 if ( !file_put_contents( $this->
cachePath(), $text, LOCK_EX ) ) {
177 $this->mCached =
null;
182 $this->mCached =
true;
194 $this->mCached =
false;
213 return $this->mType .
'/';
225 if ( $fileCacheDepth > 0 ) {
226 $hash = md5( $this->mKey );
227 for ( $i = 1; $i <= $fileCacheDepth; $i++ ) {
228 $subdir .= substr( $hash, 0, $i ) .
'/';
241 if ( mt_rand( 1, self::MISS_FACTOR ) == 1 ) {
242 # Get a large IP range that should include the user even if that
243 # person's IP address changes
244 $ip = $request->
getIP();
245 if ( !IPUtils::isValid( $ip ) ) {
249 $ip = IPUtils::isIPv6( $ip )
250 ? IPUtils::sanitizeRange(
"$ip/32" )
251 : IPUtils::sanitizeRange(
"$ip/16" );
253 # Bail out if a request already came from this range...
255 ->getLocalClusterInstance();
256 $key = $cache->makeKey( static::class,
'attempt', $this->mType, $this->mKey, $ip );
257 if ( !$cache->add( $key, 1, self::MISS_TTL_SEC ) ) {
261 # Increment the number of cache misses...
262 $cache->incrWithInit( $this->
cacheMissKey( $cache ), self::MISS_TTL_SEC );
272 ->getLocalClusterInstance();
274 return self::MISS_FACTOR * $cache->get( $this->
cacheMissKey( $cache ) );
282 return $cache->
makeKey( static::class,
'misses', $this->mType, $this->mKey );
287class_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.
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
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()