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 $wgFileCacheDirectory;
32 return $wgFileCacheDirectory;
36 * Get the base cache directory (not specific to this file)
39 abstract protected function cacheDirectory();
42 * Get the path to the cache file
45 protected function cachePath() {
46 if ( $this->mFilePath
!== null ) {
47 return $this->mFilePath
;
50 $dir = $this->cacheDirectory();
51 # Build directories (methods include the trailing "/")
52 $subDirs = $this->typeSubdirectory() . $this->hashSubdirectory();
53 # Avoid extension confusion
54 $key = str_replace( '.', '%2E', urlencode( $this->mKey
) );
55 # Build the full file path
56 $this->mFilePath
= "{$dir}/{$subDirs}{$key}.{$this->mExt}";
57 if ( $this->useGzip() ) {
58 $this->mFilePath
.= '.gz';
61 return $this->mFilePath
;
65 * Check if the cache file exists
68 public function isCached() {
69 if ( $this->mCached
=== null ) {
70 $this->mCached
= file_exists( $this->cachePath() );
72 return $this->mCached
;
76 * Get the last-modified timestamp of the cache file
77 * @return string|bool TS_MW timestamp
79 public function cacheTimestamp() {
80 $timestamp = filemtime( $this->cachePath() );
81 return ( $timestamp !== false )
82 ?
wfTimestamp( TS_MW
, $timestamp )
87 * Check if up to date cache file exists
88 * @param $timestamp string MW_TS timestamp
92 public function isCacheGood( $timestamp = '' ) {
95 if ( !$this->isCached() ) {
99 $cachetime = $this->cacheTimestamp();
100 $good = ( $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime );
101 wfDebug( __METHOD__
. ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n" );
107 * Check if the cache is gzipped
110 protected function useGzip() {
111 return $this->mUseGzip
;
115 * Get the uncompressed text from the cache
118 public function fetchText() {
119 if( $this->useGzip() ) {
120 $fh = gzopen( $this->cachePath(), 'rb' );
121 return stream_get_contents( $fh );
123 return file_get_contents( $this->cachePath() );
128 * Save and compress text to the cache
129 * @return string compressed text
131 public function saveText( $text ) {
132 global $wgUseFileCache;
134 if ( !$wgUseFileCache ) {
138 if ( $this->useGzip() ) {
139 $text = gzencode( $text );
142 $this->checkCacheDirs(); // build parent dir
143 if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX
) ) {
144 wfDebug( __METHOD__
. "() failed saving ". $this->cachePath() . "\n");
145 $this->mCached
= null;
149 $this->mCached
= true;
154 * Clear the cache for this page
157 public function clearCache() {
158 wfSuppressWarnings();
159 unlink( $this->cachePath() );
161 $this->mCached
= false;
165 * Create parent directors of $this->cachePath()
168 protected function checkCacheDirs() {
169 wfMkdirParents( dirname( $this->cachePath() ), null, __METHOD__
);
173 * Get the cache type subdirectory (with trailing slash)
174 * An extending class could use that method to alter the type -> directory
175 * mapping. @see HTMLFileCache::typeSubdirectory() for an example.
179 protected function typeSubdirectory() {
180 return $this->mType
. '/';
184 * Return relative multi-level hash subdirectory (with trailing slash)
185 * or the empty string if not $wgFileCacheDepth
188 protected function hashSubdirectory() {
189 global $wgFileCacheDepth;
192 if ( $wgFileCacheDepth > 0 ) {
193 $hash = md5( $this->mKey
);
194 for ( $i = 1; $i <= $wgFileCacheDepth; $i++
) {
195 $subdir .= substr( $hash, 0, $i ) . '/';
203 * Roughly increments the cache misses in the last hour by unique visitors
204 * @param $request WebRequest
207 public function incrMissesRecent( WebRequest
$request ) {
209 if ( mt_rand( 0, self
::MISS_FACTOR
- 1 ) == 0 ) {
210 # Get a large IP range that should include the user even if that
211 # person's IP address changes
212 $ip = $request->getIP();
213 if ( !IP
::isValid( $ip ) ) {
216 $ip = IP
::isIPv6( $ip )
217 ? IP
::sanitizeRange( "$ip/32" )
218 : IP
::sanitizeRange( "$ip/16" );
220 # Bail out if a request already came from this range...
221 $key = wfMemcKey( get_class( $this ), 'attempt', $this->mType
, $this->mKey
, $ip );
222 if ( $wgMemc->get( $key ) ) {
223 return; // possibly the same user
225 $wgMemc->set( $key, 1, self
::MISS_TTL_SEC
);
227 # Increment the number of cache misses...
228 $key = $this->cacheMissKey();
229 if ( $wgMemc->get( $key ) === false ) {
230 $wgMemc->set( $key, 1, self
::MISS_TTL_SEC
);
232 $wgMemc->incr( $key );
238 * Roughly gets the cache misses in the last hour by unique visitors
241 public function getMissesRecent() {
243 return self
::MISS_FACTOR
* $wgMemc->get( $this->cacheMissKey() );
249 protected function cacheMissKey() {
250 return wfMemcKey( get_class( $this ), 'misses', $this->mType
, $this->mKey
);