MediaWiki
REL1_37
ResourceFileCache.php
Go to the documentation of this file.
1
<?php
29
class
ResourceFileCache
extends
FileCacheBase
{
30
protected
$mCacheWorthy
;
31
32
/* @todo configurable? */
33
private
const
MISS_THRESHOLD
= 360;
// 6/min * 60 min
34
40
public
static
function
newFromContext
(
ResourceLoaderContext
$context ) {
41
$cache
=
new
self
();
42
43
if
( $context->
getImage
() ) {
44
$cache
->mType =
'image'
;
45
} elseif ( $context->
getOnly
() ===
'styles'
) {
46
$cache
->mType =
'css'
;
47
}
else
{
48
$cache
->mType =
'js'
;
49
}
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
55
}
56
57
return
$cache
;
58
}
59
66
public
static
function
useFileCache
(
ResourceLoaderContext
$context ) {
67
global
$wgUseFileCache
,
$wgDefaultSkin
,
$wgLanguageCode
;
68
if
( !
$wgUseFileCache
) {
69
return
false
;
70
}
71
// Get all query values
72
$queryVals = $context->
getRequest
()->getValues();
73
foreach
( $queryVals as $query => $val ) {
74
if
( in_array( $query, [
'modules'
,
'image'
,
'variant'
,
'version'
] ) ) {
75
// Use file cache regardless of the value of this parameter
76
continue
;
77
} elseif ( $query ===
'skin'
&& $val ===
$wgDefaultSkin
) {
78
continue
;
79
} elseif ( $query ===
'lang'
&& $val ===
$wgLanguageCode
) {
80
continue
;
81
} elseif ( $query ===
'only'
&& in_array( $val, [
'styles'
,
'scripts'
] ) ) {
82
continue
;
83
} elseif ( $query ===
'debug'
&& $val ===
'false'
) {
84
continue
;
85
} elseif ( $query ===
'format'
&& $val ===
'rasterized'
) {
86
continue
;
87
}
88
89
return
false
;
90
}
91
92
return
true
;
// cacheable
93
}
94
99
protected
function
cacheDirectory
() {
100
return
$this->
baseCacheDirectory
() .
'/resources'
;
101
}
102
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
112
);
113
}
114
115
return
$this->mCacheWorthy
;
116
}
117
}
$wgLanguageCode
$wgLanguageCode
Site language code.
Definition
DefaultSettings.php:3442
$wgDefaultSkin
$wgDefaultSkin
Default skin, for new users and anonymous visitors.
Definition
DefaultSettings.php:3827
$wgUseFileCache
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites.
Definition
DefaultSettings.php:3116
$modules
$modules
Definition
HTMLFormElement.php:15
FileCacheBase
Base class for data storage in the file system.
Definition
FileCacheBase.php:31
FileCacheBase\getMissesRecent
getMissesRecent()
Roughly gets the cache misses in the last hour by unique visitors.
Definition
FileCacheBase.php:262
FileCacheBase\baseCacheDirectory
baseCacheDirectory()
Get the base file cache directory.
Definition
FileCacheBase.php:54
FileCacheBase\isCached
isCached()
Check if the cache file exists.
Definition
FileCacheBase.php:93
ResourceFileCache
ResourceLoader request result caching in the file system.
Definition
ResourceFileCache.php:29
ResourceFileCache\cacheDirectory
cacheDirectory()
Get the base file cache directory.
Definition
ResourceFileCache.php:99
ResourceFileCache\$mCacheWorthy
$mCacheWorthy
Definition
ResourceFileCache.php:30
ResourceFileCache\MISS_THRESHOLD
const MISS_THRESHOLD
Definition
ResourceFileCache.php:33
ResourceFileCache\useFileCache
static useFileCache(ResourceLoaderContext $context)
Check if an RL request can be cached.
Definition
ResourceFileCache.php:66
ResourceFileCache\isCacheWorthy
isCacheWorthy()
Item has many recent cache misses.
Definition
ResourceFileCache.php:107
ResourceFileCache\newFromContext
static newFromContext(ResourceLoaderContext $context)
Construct an ResourceFileCache from a context.
Definition
ResourceFileCache.php:40
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition
ResourceLoaderContext.php:34
ResourceLoaderContext\getRequest
getRequest()
Definition
ResourceLoaderContext.php:165
ResourceLoaderContext\getImage
getImage()
Definition
ResourceLoaderContext.php:292
ResourceLoaderContext\getHash
getHash()
All factors that uniquely identify this request, except 'modules'.
Definition
ResourceLoaderContext.php:384
ResourceLoaderContext\getModules
getModules()
Definition
ResourceLoaderContext.php:179
ResourceLoaderContext\getOnly
getOnly()
Definition
ResourceLoaderContext.php:272
$cache
$cache
Definition
mcc.php:33
includes
cache
ResourceFileCache.php
Generated on Fri Apr 5 2024 23:40:06 for MediaWiki by
1.9.8