3 * Contain the FileCacheBase class
7 abstract class FileCacheBase
{
9 protected $mType = 'object';
10 protected $mExt = 'cache';
14 /* @TODO: configurable? */
15 const MISS_FACTOR
= 10; // log 1 every MISS_FACTOR cache misses
17 protected function __construct() {
20 $this->mUseGzip
= (bool)$wgUseGzip;
24 * Get the base file cache directory
27 final protected function baseCacheDirectory() {
28 global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
29 if ( $wgFileCacheDirectory ) {
30 $dir = $wgFileCacheDirectory;
31 } elseif ( $wgCacheDirectory ) {
32 $dir = $wgCacheDirectory;
34 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
40 * Get the base cache directory (not speficic to this file)
43 abstract protected function cacheDirectory();
46 * Get the path to the cache file
49 protected function cachePath() {
50 if ( $this->mFilePath
!== null ) {
51 return $this->mFilePath
;
54 $dir = $this->cacheDirectory();
55 # Build directories (methods include the trailing "/")
56 $subDirs = $this->typeSubdirectory() . $this->hashSubdirectory();
57 # Avoid extension confusion
58 $key = str_replace( '.', '%2E', urlencode( $this->mKey
) );
59 # Build the full file path
60 $this->mFilePath
= "{$dir}/{$subDirs}{$key}.{$this->mExt}";
61 if ( $this->useGzip() ) {
62 $this->mFilePath
.= '.gz';
65 return $this->mFilePath
;
69 * Check if the cache file exists
72 public function isCached() {
73 return file_exists( $this->cachePath() );
77 * Get the last-modified timestamp of the cache file
78 * @return string|false TS_MW timestamp
80 public function cacheTimestamp() {
81 $timestamp = filemtime( $this->cachePath() );
82 return ( $timestamp !== false )
83 ?
wfTimestamp( TS_MW
, $timestamp )
88 * Check if up to date cache file exists
89 * @param $timestamp string MW_TS timestamp
93 public function isCacheGood( $timestamp = '' ) {
96 if ( !$this->isCached() ) {
100 $cachetime = $this->cacheTimestamp();
101 $good = ( $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime );
102 wfDebug( __METHOD__
. ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
108 * Check if the cache is gzipped
111 protected function useGzip() {
112 return $this->mUseGzip
;
116 * Get the uncompressed text from the cache
119 public function fetchText() {
120 if ( $this->useGzip() ) {
121 /* Why is there no gzfile_get_contents() or gzdecode()? */
122 return implode( '', gzfile( $this->cachePath() ) );
124 return file_get_contents( $this->cachePath() );
129 * Save and compress text to the cache
130 * @return string compressed text
132 public function saveText( $text ) {
133 global $wgUseFileCache;
135 if ( !$wgUseFileCache ) {
139 if ( $this->useGzip() ) {
140 $text = gzencode( $text );
143 $this->checkCacheDirs(); // build parent dir
144 if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX
) ) {
152 * Clear the cache for this page
155 public function clearCache() {
156 wfSuppressWarnings();
157 unlink( $this->cachePath() );
162 * Create parent directors of $this->cachePath()
165 protected function checkCacheDirs() {
166 wfMkdirParents( dirname( $this->cachePath() ), null, __METHOD__
);
170 * Get the cache type subdirectory (with trailing slash) or the empty string
173 protected function typeSubdirectory() {
174 return $this->mType
. '/';
178 * Return relative multi-level hash subdirectory (with trailing slash)
179 * or the empty string if not $wgFileCacheDepth
182 protected function hashSubdirectory() {
183 global $wgFileCacheDepth;
186 if ( $wgFileCacheDepth > 0 ) {
187 $hash = md5( $this->mKey
);
188 for ( $i = 1; $i <= $wgFileCacheDepth; $i++
) {
189 $subdir .= substr( $hash, 0, $i ) . '/';
197 * Roughly increments the cache misses in the last hour by unique visitors
198 * @param $request WebRequest
201 public function incrMissesRecent( WebRequest
$request ) {
203 if ( mt_rand( 0, self
::MISS_FACTOR
- 1 ) == 0 ) {
204 # Get an large IP range that should include the user
205 # even if that person's IP address changes...
206 $ip = $request->getIP();
207 if ( !IP
::isValid( $ip ) ) {
210 $ip = IP
::isIPv6( $ip )
211 ? IP
::sanitizeRange( "$ip/64" )
212 : IP
::sanitizeRange( "$ip/16" );
214 # Bail out if a request already came from this range...
215 $key = wfMemcKey( get_class( $this ), 'attempt', $this->mType
, $this->mKey
, $ip );
216 if ( $wgMemc->get( $key ) ) {
217 return; // possibly the same user
219 $wgMemc->set( $key, 1, 3600 );
221 # Increment the number of cache misses...
222 $key = $this->cacheMissKey();
223 if ( $wgMemc->get( $key ) === false ) {
224 $wgMemc->set( $key, 1, 3600 );
226 $wgMemc->incr( $key );
232 * Roughly gets the cache misses in the last hour by unique visitors
235 public function getMissesRecent() {
237 return self
::MISS_FACTOR
* $wgMemc->get( $this->cacheMissKey() );
243 protected function cacheMissKey() {
244 return wfMemcKey( get_class( $this ), 'misses', $this->mType
, $this->mKey
);