MediaWiki  master
ResourceFileCache.php
Go to the documentation of this file.
1 <?php
2 
28 
35  protected $mCacheWorthy;
36 
37  /* @todo configurable? */
38  private const MISS_THRESHOLD = 360; // 6/min * 60 min
39 
45  public static function newFromContext( RL\Context $context ) {
46  $cache = new self();
47 
48  if ( $context->getImage() ) {
49  $cache->mType = 'image';
50  } elseif ( $context->getOnly() === 'styles' ) {
51  $cache->mType = 'css';
52  } else {
53  $cache->mType = 'js';
54  }
55  $modules = array_unique( $context->getModules() ); // remove duplicates
56  sort( $modules ); // normalize the order (permutation => combination)
57  $cache->mKey = sha1( $context->getHash() . implode( '|', $modules ) );
58  if ( count( $modules ) == 1 ) {
59  $cache->mCacheWorthy = true; // won't take up much space
60  }
61 
62  return $cache;
63  }
64 
71  public static function useFileCache( RL\Context $context ) {
72  $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
73  $useFileCache = $mainConfig->get( MainConfigNames::UseFileCache );
74  $defaultSkin = $mainConfig->get( MainConfigNames::DefaultSkin );
75  $languageCode = $mainConfig->get( MainConfigNames::LanguageCode );
76  if ( !$useFileCache ) {
77  return false;
78  }
79  // Get all query values
80  $queryVals = $context->getRequest()->getValues();
81  foreach ( $queryVals as $query => $val ) {
82  if ( in_array( $query, [ 'modules', 'image', 'variant', 'version' ] ) ) {
83  // Use file cache regardless of the value of this parameter
84  continue;
85  } elseif ( $query === 'skin' && $val === $defaultSkin ) {
86  continue;
87  } elseif ( $query === 'lang' && $val === $languageCode ) {
88  continue;
89  } elseif ( $query === 'only' && in_array( $val, [ 'styles', 'scripts' ] ) ) {
90  continue;
91  } elseif ( $query === 'debug' && $val === 'false' ) {
92  continue;
93  } elseif ( $query === 'format' && $val === 'rasterized' ) {
94  continue;
95  }
96 
97  return false;
98  }
99 
100  return true; // cacheable
101  }
102 
107  protected function cacheDirectory() {
108  return $this->baseCacheDirectory() . '/resources';
109  }
110 
115  public function isCacheWorthy() {
116  if ( $this->mCacheWorthy === null ) {
117  $this->mCacheWorthy = (
118  $this->isCached() || // even stale cache indicates it was cache worthy
119  $this->getMissesRecent() >= self::MISS_THRESHOLD // many misses
120  );
121  }
122 
123  return $this->mCacheWorthy;
124  }
125 }
$modules
Base class for data storage in the file system.
getMissesRecent()
Roughly gets the cache misses in the last hour by unique visitors.
baseCacheDirectory()
Get the base file cache directory.
isCached()
Check if the cache file exists.
A class containing constants representing the names of configuration variables.
MediaWikiServices is the service locator for the application scope of MediaWiki.
ResourceLoader request result caching in the file system.
cacheDirectory()
Get the base file cache directory.
static useFileCache(RL\Context $context)
Check if an RL request can be cached.
static newFromContext(RL\Context $context)
Construct an ResourceFileCache from a context.
isCacheWorthy()
Item has many recent cache misses.
$cache
Definition: mcc.php:33