3 * Data storage in the file system.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Cache
;
26 use MediaWiki\Config\ServiceOptions
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\MediaWikiServices
;
29 use MediaWiki\Request\WebRequest
;
30 use Wikimedia\AtEase\AtEase
;
31 use Wikimedia\IPUtils
;
32 use Wikimedia\ObjectCache\BagOStuff
;
35 * Base class for data storage in the file system.
39 abstract class FileCacheBase
{
41 private const CONSTRUCTOR_OPTIONS
= [
42 MainConfigNames
::CacheEpoch
,
43 MainConfigNames
::FileCacheDepth
,
44 MainConfigNames
::FileCacheDirectory
,
45 MainConfigNames
::MimeType
,
46 MainConfigNames
::UseGzip
,
52 protected $mType = 'object';
54 protected $mExt = 'cache';
55 /** @var string|null */
59 /** @var bool|null lazy loaded */
61 /** @var ServiceOptions */
64 /* @todo configurable? */
65 private const MISS_FACTOR
= 15; // log 1 every MISS_FACTOR cache misses
66 private const MISS_TTL_SEC
= 3600; // how many seconds ago is "recent"
68 protected function __construct() {
69 $this->options
= new ServiceOptions(
70 self
::CONSTRUCTOR_OPTIONS
,
71 MediaWikiServices
::getInstance()->getMainConfig()
73 $this->mUseGzip
= (bool)$this->options
->get( MainConfigNames
::UseGzip
);
77 * Get the base file cache directory
80 final protected function baseCacheDirectory() {
81 return $this->options
->get( MainConfigNames
::FileCacheDirectory
);
85 * Get the base cache directory (not specific to this file)
88 abstract protected function cacheDirectory();
91 * Get the path to the cache file
94 protected function cachePath() {
95 if ( $this->mFilePath
!== null ) {
96 return $this->mFilePath
;
99 $dir = $this->cacheDirectory();
100 # Build directories (methods include the trailing "/")
101 $subDirs = $this->typeSubdirectory() . $this->hashSubdirectory();
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}";
106 if ( $this->useGzip() ) {
107 $this->mFilePath
.= '.gz';
110 return $this->mFilePath
;
114 * Check if the cache file exists
117 public function isCached() {
118 $this->mCached ??
= is_file( $this->cachePath() );
120 return $this->mCached
;
124 * Get the last-modified timestamp of the cache file
125 * @return string|bool TS_MW timestamp
127 public function cacheTimestamp() {
128 $timestamp = filemtime( $this->cachePath() );
130 return ( $timestamp !== false )
131 ?
wfTimestamp( TS_MW
, $timestamp )
136 * Check if up to date cache file exists
137 * @param string $timestamp MW_TS timestamp
141 public function isCacheGood( $timestamp = '' ) {
142 $cacheEpoch = $this->options
->get( MainConfigNames
::CacheEpoch
);
144 if ( !$this->isCached() ) {
148 $cachetime = $this->cacheTimestamp();
149 $good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime );
150 wfDebug( __METHOD__
.
151 ": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " . wfBoolToStr( $good ) );
157 * Check if the cache is gzipped
160 protected function useGzip() {
161 return $this->mUseGzip
;
165 * Get the uncompressed text from the cache
168 public function fetchText() {
169 if ( $this->useGzip() ) {
170 $fh = gzopen( $this->cachePath(), 'rb' );
172 return stream_get_contents( $fh );
174 return file_get_contents( $this->cachePath() );
179 * Save and compress text to the cache
180 * @param string $text
181 * @return string|false Compressed text
183 public function saveText( $text ) {
184 if ( $this->useGzip() ) {
185 $text = gzencode( $text );
188 $this->checkCacheDirs(); // build parent dir
189 if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX
) ) {
190 wfDebug( __METHOD__
. "() failed saving " . $this->cachePath() );
191 $this->mCached
= null;
196 $this->mCached
= true;
202 * Clear the cache for this page
205 public function clearCache() {
206 AtEase
::suppressWarnings();
207 unlink( $this->cachePath() );
208 AtEase
::restoreWarnings();
209 $this->mCached
= false;
213 * Create parent directors of $this->cachePath()
216 protected function checkCacheDirs() {
217 wfMkdirParents( dirname( $this->cachePath() ), null, __METHOD__
);
221 * Get the cache type subdirectory (with trailing slash)
222 * An extending class could use that method to alter the type -> directory
223 * mapping. @see HTMLFileCache::typeSubdirectory() for an example.
227 protected function typeSubdirectory() {
228 return $this->mType
. '/';
232 * Return relative multi-level hash subdirectory (with trailing slash)
233 * or the empty string if not $wgFileCacheDepth
236 protected function hashSubdirectory() {
237 $fileCacheDepth = $this->options
->get( MainConfigNames
::FileCacheDepth
);
240 if ( $fileCacheDepth > 0 ) {
241 $hash = md5( $this->mKey
);
242 for ( $i = 1; $i <= $fileCacheDepth; $i++
) {
243 $subdir .= substr( $hash, 0, $i ) . '/';
251 * Roughly increments the cache misses in the last hour by unique visitors
252 * @param WebRequest $request
255 public function incrMissesRecent( WebRequest
$request ) {
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...
269 $cache = MediaWikiServices
::getInstance()->getObjectCacheFactory()
270 ->getLocalClusterInstance();
271 $key = $cache->makeKey( static::class, 'attempt', $this->mType
, $this->mKey
, $ip );
272 if ( !$cache->add( $key, 1, self
::MISS_TTL_SEC
) ) {
273 return; // possibly the same user
276 # Increment the number of cache misses...
277 $cache->incrWithInit( $this->cacheMissKey( $cache ), self
::MISS_TTL_SEC
);
282 * Roughly gets the cache misses in the last hour by unique visitors
285 public function getMissesRecent() {
286 $cache = MediaWikiServices
::getInstance()->getObjectCacheFactory()
287 ->getLocalClusterInstance();
289 return self
::MISS_FACTOR
* $cache->get( $this->cacheMissKey( $cache ) );
293 * @param BagOStuff $cache Instance that the key will be used with
296 protected function cacheMissKey( BagOStuff
$cache ) {
297 return $cache->makeKey( static::class, 'misses', $this->mType
, $this->mKey
);
301 /** @deprecated class alias since 1.42 */
302 class_alias( FileCacheBase
::class, 'FileCacheBase' );