[FileBackend] Added getScopedLocksForOps() function.
[lhc/web/wiklou.git] / includes / cache / ResourceFileCache.php
1 <?php
2 /**
3 * Contain the ResourceFileCache class
4 * @file
5 * @ingroup Cache
6 */
7 class ResourceFileCache extends FileCacheBase {
8 protected $mCacheWorthy;
9
10 /* @TODO: configurable? */
11 const MISS_THRESHOLD = 360; // 6/min * 60 min
12
13 /**
14 * Construct an ResourceFileCache from a context
15 * @param $context ResourceLoaderContext
16 * @return ResourceFileCache
17 */
18 public static function newFromContext( ResourceLoaderContext $context ) {
19 $cache = new self();
20
21 if ( $context->getOnly() === 'styles' ) {
22 $cache->mType = 'css';
23 } else {
24 $cache->mType = 'js';
25 }
26 $modules = array_unique( $context->getModules() ); // remove duplicates
27 sort( $modules ); // normalize the order (permutation => combination)
28 $cache->mKey = sha1( $context->getHash() . implode( '|', $modules ) );
29 if ( count( $modules ) == 1 ) {
30 $cache->mCacheWorthy = true; // won't take up much space
31 }
32
33 return $cache;
34 }
35
36 /**
37 * Check if an RL request can be cached.
38 * Caller is responsible for checking if any modules are private.
39 * @param $context ResourceLoaderContext
40 * @return bool
41 */
42 public static function useFileCache( ResourceLoaderContext $context ) {
43 global $wgUseFileCache, $wgDefaultSkin, $wgLanguageCode;
44 if ( !$wgUseFileCache ) {
45 return false;
46 }
47 // Get all query values
48 $queryVals = $context->getRequest()->getValues();
49 foreach ( $queryVals as $query => $val ) {
50 if ( $query === 'modules' || $query === 'version' || $query === '*' ) {
51 continue; // note: &* added as IE fix
52 } elseif ( $query === 'skin' && $val === $wgDefaultSkin ) {
53 continue;
54 } elseif ( $query === 'lang' && $val === $wgLanguageCode ) {
55 continue;
56 } elseif ( $query === 'only' && in_array( $val, array( 'styles', 'scripts' ) ) ) {
57 continue;
58 } elseif ( $query === 'debug' && $val === 'false' ) {
59 continue;
60 }
61 return false;
62 }
63 return true; // cacheable
64 }
65
66 /**
67 * Get the base file cache directory
68 * @return string
69 */
70 protected function cacheDirectory() {
71 return $this->baseCacheDirectory() . '/resources';
72 }
73
74 /**
75 * Item has many recent cache misses
76 * @return bool
77 */
78 public function isCacheWorthy() {
79 if ( $this->mCacheWorthy === null ) {
80 $this->mCacheWorthy = (
81 $this->isCached() || // even stale cache indicates it was cache worthy
82 $this->getMissesRecent() >= self::MISS_THRESHOLD // many misses
83 );
84 }
85 return $this->mCacheWorthy;
86 }
87 }