3 * ResourceLoader request result caching 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
25 * ResourceLoader request result caching in the file system.
29 class ResourceFileCache
extends FileCacheBase
{
30 protected $mCacheWorthy;
32 /* @todo configurable? */
33 const MISS_THRESHOLD
= 360; // 6/min * 60 min
36 * Construct an ResourceFileCache from a context
37 * @param ResourceLoaderContext $context
38 * @return ResourceFileCache
40 public static function newFromContext( ResourceLoaderContext
$context ) {
43 if ( $context->getImage() ) {
44 $cache->mType
= 'image';
45 } elseif ( $context->getOnly() === 'styles' ) {
46 $cache->mType
= 'css';
50 $modules = array_unique( $context->getModules() ); // remove duplicates
51 sort( $modules ); // normalize the order (permutation => combination)
52 $cache->mKey
= sha1( $context->getHash() . implode( '|', $modules ) );
53 if ( count( $modules ) == 1 ) {
54 $cache->mCacheWorthy
= true; // won't take up much space
61 * Check if an RL request can be cached.
62 * Caller is responsible for checking if any modules are private.
63 * @param ResourceLoaderContext $context
66 public static function useFileCache( ResourceLoaderContext
$context ) {
67 global $wgUseFileCache, $wgDefaultSkin, $wgLanguageCode;
68 if ( !$wgUseFileCache ) {
71 // Get all query values
72 $queryVals = $context->getRequest()->getValues();
73 foreach ( $queryVals as $query => $val ) {
74 if ( in_array( $query, array( 'modules', 'image', 'variant', 'version', '*' ) ) ) {
75 // Use file cache regardless of the value of this parameter
76 continue; // note: &* added as IE fix
77 } elseif ( $query === 'skin' && $val === $wgDefaultSkin ) {
79 } elseif ( $query === 'lang' && $val === $wgLanguageCode ) {
81 } elseif ( $query === 'only' && in_array( $val, array( 'styles', 'scripts' ) ) ) {
83 } elseif ( $query === 'debug' && $val === 'false' ) {
85 } elseif ( $query === 'format' && $val === 'rasterized' ) {
92 return true; // cacheable
96 * Get the base file cache directory
99 protected function cacheDirectory() {
100 return $this->baseCacheDirectory() . '/resources';
104 * Item has many recent cache misses
107 public function isCacheWorthy() {
108 if ( $this->mCacheWorthy
=== null ) {
109 $this->mCacheWorthy
= (
110 $this->isCached() ||
// even stale cache indicates it was cache worthy
111 $this->getMissesRecent() >= self
::MISS_THRESHOLD
// many misses
115 return $this->mCacheWorthy
;