r113800 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113799‎ | r113800 | r113801 >
Date:08:34, 14 March 2012
Author:santhosh
Status:ok (Comments)
Tags:i18nreview 
Comment:
Impliment getModifiedTime for ResourceLoaderLanguageDataModule, using CACHE_ANYTHING, store md5 hash of the grammarforms
and compare the hash to see if it is modified.
(Code mostly written by Niklas)
Ping r112991
Modified paths:
  • /branches/jsgrammar/includes/resourceloader/ResourceLoaderLanguageDataModule.php (modified) (history)

Diff [purge]

Index: branches/jsgrammar/includes/resourceloader/ResourceLoaderLanguageDataModule.php
@@ -53,28 +53,21 @@
5454 * @return array|int|Mixed
5555 */
5656 public function getModifiedTime( ResourceLoaderContext $context ) {
57 - global $wgCacheEpoch;
 57+ $cache = wfGetCache( CACHE_ANYTHING );
 58+ $key = wfMemckey( 'rllangdatacache' );
5859
59 - /**
60 - * @todo FIXME: This needs to change whenever the array created by
61 - * $wgContLang->getGrammarForms() changes. Which gets its data from
62 - * $wgGrammarForms, which (for standard installations) comes from LocalSettings
63 - * and $wgCacheEpoch would cover that. However there's two three problems:
64 - *
65 - * 1) $wgCacheEpoch is not meant for this use.
66 - * 2) If $wgInvalidateCacheOnLocalSettingsChange is set to false,
67 - * $wgCacheEpoch will not be raised if LocalSettings is modified (see #1).
68 - * 3) $wgGrammarForms can be set from anywhere. For example on WMF it is set
69 - * by the WikimediaMessages extension. Other farms might set it form
70 - * their 'CommonSettings.php'-like file or something (see #1).
71 - *
72 - * Possible solutions:
73 - * - Store grammarforms in the language object cache instead of directly
74 - * from the global everytime. Then use $wgContLang->getLastModified().
75 - * - Somehow monitor the value of $wgGrammarForms.
76 - */
 60+ $forms = $this->getSiteLangGrammarForms();
 61+ $hash = md5( serialize( $forms ) );
7762
78 - return $wgCacheEpoch;
 63+ $result = $cache->get( $key );
 64+ if ( is_array( $result ) ) {
 65+ if ( $result['hash'] === $hash ) {
 66+ return $result['timestamp'];
 67+ }
 68+ }
 69+ $timestamp = wfTimestamp();
 70+ $cache->set( $key, array( 'hash' => $hash, 'timestamp' => $timestamp ) );
 71+ return $timestamp;
7972 }
8073
8174 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r113806Change the cache key as suggested by Timo in r113800...santhosh11:27, 14 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r111723First version of ResourceLoaderLanguageModule....santhosh04:30, 17 February 2012
r112991[JSGrammar] Various minor tips, tricks and clean up....krinkle23:56, 4 March 2012

Comments

#Comment by Krinkle (talk | contribs)   11:00, 14 March 2012
+		$key = wfMemckey( 'rllangdatacache' );

wfMemcKey takes several arguments to create a fake-hierarchy in the cache. Most calls in ResourceLoader use 'resourceloader', 'group', 'exact key', 'optional variation'. Suggesting something like wfMemcKey( 'resourceloader', 'langdatamodule', 'changeinfo' ); (not using langdatacache since it's not a cache of the language data itself, but of the hash and a timestamp).

Also the function is called wfMemcKey, makes it easier to find with standard ack-grep :)

#Comment by Nikerabbit (talk | contribs)   19:28, 14 March 2012

For the reviewer: We discussed this together for a while and thought about using l10n cache and other caches, but those all use DepedencyWrapper which doesn't provide way to get timestamps of last change (some Dependency objects don't even store that), and using file timestamps doesn't work if l10n cache is in the database. And GlobalDependency stores the actual global value in cache, which in this case is unnecessarily large, so we went with a hash, which hopefully is fast enough for this purpose.

#Comment by Krinkle (talk | contribs)   00:47, 15 March 2012

Yes, I recognized the logic from GlobalDependency, but without the unnecessary overhead and with the timestamp. This is exactly what I had in mind, very nice :)

Perhaps use json_encode though, instead of serialize() (seems a little faster, especially for larger arrays).

Marking OK.

Status & tagging log