32use Wikimedia\AtEase\AtEase;
42 private const CONSTRUCTOR_OPTIONS = [
61 private const MISS_FACTOR = 15;
62 private const MISS_TTL_SEC = 3600;
66 self::CONSTRUCTOR_OPTIONS,
91 if ( $this->mFilePath !==
null ) {
96 # Build directories (methods include the trailing "/")
98 # Avoid extension confusion
99 $key = str_replace(
'.',
'%2E', urlencode( $this->mKey ) );
100 # Build the full file path
101 $this->mFilePath =
"{$dir}/{$subDirs}{$key}.{$this->mExt}";
103 $this->mFilePath .=
'.gz';
114 $this->mCached ??= is_file( $this->
cachePath() );
124 $timestamp = filemtime( $this->
cachePath() );
126 return ( $timestamp !==
false )
145 $good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime );
147 ": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " .
wfBoolToStr( $good ) );
166 $fh = gzopen( $this->
cachePath(),
'rb' );
168 return stream_get_contents( $fh );
170 return file_get_contents( $this->
cachePath() );
181 $text = gzencode( $text );
185 if ( !file_put_contents( $this->
cachePath(), $text, LOCK_EX ) ) {
187 $this->mCached =
null;
192 $this->mCached =
true;
202 AtEase::suppressWarnings();
204 AtEase::restoreWarnings();
205 $this->mCached =
false;
224 return $this->mType .
'/';
236 if ( $fileCacheDepth > 0 ) {
237 $hash = md5( $this->mKey );
238 for ( $i = 1; $i <= $fileCacheDepth; $i++ ) {
239 $subdir .= substr( $hash, 0, $i ) .
'/';
252 if ( mt_rand( 0, self::MISS_FACTOR - 1 ) == 0 ) {
253 # Get a large IP range that should include the user even if that
254 # person's IP address changes
255 $ip = $request->
getIP();
256 if ( !IPUtils::isValid( $ip ) ) {
260 $ip = IPUtils::isIPv6( $ip )
261 ? IPUtils::sanitizeRange(
"$ip/32" )
262 : IPUtils::sanitizeRange(
"$ip/16" );
264 # Bail out if a request already came from this range...
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 );
283 return self::MISS_FACTOR * $cache->get( $this->
cacheMissKey( $cache ) );
291 return $cache->
makeKey( static::class,
'misses', $this->mType, $this->mKey );
296class_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.
Class representing a cache/ephemeral data store.
makeKey( $keygroup,... $components)
Make a cache key from the given components, in the default keyspace.
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()
Functions to get cache objects.
static getLocalClusterInstance()
Get the main cluster-local cache object.