e6be049335272adc01710c95d9fed98467fc95d8
3 * Contain the FileCacheBase class
7 abstract class FileCacheBase
{
9 protected $mType = 'object';
10 protected $mExt = 'cache';
16 /* @TODO: configurable? */
17 const MISS_FACTOR
= 15; // log 1 every MISS_FACTOR cache misses
18 const MISS_TTL_SEC
= 3600; // how many seconds ago is "recent"
20 protected function __construct() {
23 $this->mUseGzip
= (bool)$wgUseGzip;
27 * Get the base file cache directory
30 final protected function baseCacheDirectory() {
31 global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
32 if ( $wgFileCacheDirectory ) {
33 $dir = $wgFileCacheDirectory;
34 } elseif ( $wgCacheDirectory ) {
35 $dir = $wgCacheDirectory;
37 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
43 * Get the base cache directory (not speficic to this file)
46 abstract protected function cacheDirectory();
49 * Get the path to the cache file
52 protected function cachePath() {
53 if ( $this->mFilePath
!== null ) {
54 return $this->mFilePath
;
57 $dir = $this->cacheDirectory();
58 # Build directories (methods include the trailing "/")
59 $subDirs = $this->typeSubdirectory() . $this->hashSubdirectory();
60 # Avoid extension confusion
61 $key = str_replace( '.', '%2E', urlencode( $this->mKey
) );
62 # Build the full file path
63 $this->mFilePath
= "{$dir}/{$subDirs}{$key}.{$this->mExt}";
64 if ( $this->useGzip() ) {
65 $this->mFilePath
.= '.gz';
68 return $this->mFilePath
;
72 * Check if the cache file exists
75 public function isCached() {
76 if ( $this->mCached
=== null ) {
77 $this->mCached
= file_exists( $this->cachePath() );
79 return $this->mCached
;
83 * Get the last-modified timestamp of the cache file
84 * @return string|false TS_MW timestamp
86 public function cacheTimestamp() {
87 $timestamp = filemtime( $this->cachePath() );
88 return ( $timestamp !== false )
89 ?
wfTimestamp( TS_MW
, $timestamp )
94 * Check if up to date cache file exists
95 * @param $timestamp string MW_TS timestamp
99 public function isCacheGood( $timestamp = '' ) {
100 global $wgCacheEpoch;
102 if ( !$this->isCached() ) {
106 $cachetime = $this->cacheTimestamp();
107 $good = ( $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime );
108 wfDebug( __METHOD__
. ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
114 * Check if the cache is gzipped
117 protected function useGzip() {
118 return $this->mUseGzip
;
122 * Get the uncompressed text from the cache
125 public function fetchText() {
126 if ( $this->useGzip() ) {
127 /* Why is there no gzfile_get_contents() or gzdecode()? */
128 return implode( '', gzfile( $this->cachePath() ) );
130 return file_get_contents( $this->cachePath() );
135 * Save and compress text to the cache
136 * @return string compressed text
138 public function saveText( $text ) {
139 global $wgUseFileCache;
141 if ( !$wgUseFileCache ) {
145 if ( $this->useGzip() ) {
146 $text = gzencode( $text );
149 $this->checkCacheDirs(); // build parent dir
150 if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX
) ) {
151 $this->mCached
= null;
155 $this->mCached
= true;
160 * Clear the cache for this page
163 public function clearCache() {
164 wfSuppressWarnings();
165 unlink( $this->cachePath() );
167 $this->mCached
= false;
171 * Create parent directors of $this->cachePath()
174 protected function checkCacheDirs() {
175 wfMkdirParents( dirname( $this->cachePath() ), null, __METHOD__
);
179 * Get the cache type subdirectory (with trailing slash) or the empty string
182 protected function typeSubdirectory() {
183 return $this->mType
. '/';
187 * Return relative multi-level hash subdirectory (with trailing slash)
188 * or the empty string if not $wgFileCacheDepth
191 protected function hashSubdirectory() {
192 global $wgFileCacheDepth;
195 if ( $wgFileCacheDepth > 0 ) {
196 $hash = md5( $this->mKey
);
197 for ( $i = 1; $i <= $wgFileCacheDepth; $i++
) {
198 $subdir .= substr( $hash, 0, $i ) . '/';
206 * Roughly increments the cache misses in the last hour by unique visitors
207 * @param $request WebRequest
210 public function incrMissesRecent( WebRequest
$request ) {
212 if ( mt_rand( 0, self
::MISS_FACTOR
- 1 ) == 0 ) {
213 # Get an large IP range that should include the user
214 # even if that person's IP address changes...
215 $ip = $request->getIP();
216 if ( !IP
::isValid( $ip ) ) {
219 $ip = IP
::isIPv6( $ip )
220 ? IP
::sanitizeRange( "$ip/64" )
221 : IP
::sanitizeRange( "$ip/16" );
223 # Bail out if a request already came from this range...
224 $key = wfMemcKey( get_class( $this ), 'attempt', $this->mType
, $this->mKey
, $ip );
225 if ( $wgMemc->get( $key ) ) {
226 return; // possibly the same user
228 $wgMemc->set( $key, 1, self
::MISS_TTL_SEC
);
230 # Increment the number of cache misses...
231 $key = $this->cacheMissKey();
232 if ( $wgMemc->get( $key ) === false ) {
233 $wgMemc->set( $key, 1, self
::MISS_TTL_SEC
);
235 $wgMemc->incr( $key );
241 * Roughly gets the cache misses in the last hour by unique visitors
244 public function getMissesRecent() {
246 return self
::MISS_FACTOR
* $wgMemc->get( $this->cacheMissKey() );
252 protected function cacheMissKey() {
253 return wfMemcKey( get_class( $this ), 'misses', $this->mType
, $this->mKey
);